Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.

There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and ak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).

Help Pashmak with the test.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

Print a single integer — the answer to the problem.

Examples

Input

7
1 2 1 1 2 2 1


Output

8


Input

3
1 1 1


Output

1


Input

5
1 2 3 4 5


Output

0
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Application {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        reader.readLine();
        int[] data = parseLine(reader);
        System.out.println(solve(data));

//        int[] data = new int[1000000];
//        Arrays.fill(data, 1000000000);
//
//        long t1 = System.currentTimeMillis();
//        System.out.println(solve(data));
//        long t2 = System.currentTimeMillis();
//        System.out.println((t2 - t1) + "ms");
    }

    private static long solve(int[] data) {
        long[] left = new long[data.length];
        long[] right = new long[data.length];

        Map<Integer, Long> cnt = new HashMap<>();
        for (int i = 0; i < left.length; i++) {
            if (!cnt.containsKey(data[i]))
                cnt.put(data[i], 1l);
            else
                cnt.put(data[i], cnt.get(data[i]) + 1);
            left[i] = cnt.get(data[i]);
        }

        cnt.clear();
        for (int i = right.length - 1; i >= 0; i--) {
            if (!cnt.containsKey(data[i]))
                cnt.put(data[i], 1l);
            else
                cnt.put(data[i], cnt.get(data[i]) + 1);
            right[i] = cnt.get(data[i]);
        }

        long res = 0;
        TreeNode root = null;
        for (int i = 0; i < right.length; i++) {
            if (i != 0) {
                res += root.moreThan(right[i]);
                root = root.add(left[i]);
            }
            else
                root = new TreeNode(left[i], null);
        }

        return res;
    }

    private static int[] parseLine(BufferedReader reader) throws IOException {
        String[] args = reader.readLine().split(" ");
        int[] res = new int[args.length];

        for (int i = 0; i < args.length; i++)
            res[i] = Integer.valueOf(args[i]);

        return res;
    }

    private static class TreeNode {

        private final long val;

        private long cnt;

        private long ownCnt;

        private int height;

        private TreeNode right;

        private TreeNode left;

        private TreeNode parent;

        public TreeNode(long val, TreeNode parent) {
            this.val = val;
            this.cnt = 1;
            this.ownCnt = 1;
            this.height = 1;
            this.parent = parent;
        }

        public TreeNode add(long val) {
            cnt++;

            if (val == this.val) {
                ownCnt++;
                return this;
            }

            if (val > this.val) {
                right = right == null ? new TreeNode(val, this) : right.add(val);
                height = Math.max(height, right.height + 1);
            }
            else {
                left = left == null ? new TreeNode(val, this) : left.add(val);
                height = Math.max(height, left.height + 1);
            }

            int rightHeigth = right == null ? 0 : right.height;
            int leftHeight = left == null ? 0 : left.height;

            if (rightHeigth - leftHeight == 2) {
                int rightLeftHeight = right.left == null ? 0 : right.left.height;
                int rightRightLength = right.right == null ? 0 : right.right.height;

                if (rightLeftHeight > rightRightLength) {
                    // case 1.
                    TreeNode root = right.left;
                    right.left = root.right;
                    root.right = right;
                    right = root.left;
                    root.left = this;

                    root.left.update();
                    root.right.update();
                    root.update();

                    return root;
                }
                else {
                    // case 2.
                    TreeNode root = right;
                    right = root.left;
                    root.left = this;

                    root.left.update();
                    root.update();

                    return root;
                }
            }
            else if (leftHeight - rightHeigth == 2) {
                int leftLeftHeight = left.left == null ? 0 : left.left.height;
                int leftRightHeight = left.right == null ? 0 : left.right.height;

                if (leftLeftHeight >= leftRightHeight) {
                    // case 2.
                    TreeNode root = left;
                    left = root.right;
                    root.right = this;

                    root.right.update();
                    root.update();

                    return root;
                }

                if (leftRightHeight > leftLeftHeight) {
                    // case 3.
                    TreeNode root = left.right;
                    left.right = root.left;
                    root.left = left;
                    left = root.right;
                    root.right = this;

                    root.left.update();
                    root.right.update();
                    root.update();

                    return root;
                }
            }

            return this;
        }

        private long moreThan(long val) {
            if (val < this.val)
                return ownCnt + (right == null ? 0l : right.cnt) + (left == null ? 0l : left.moreThan(val));

            return right == null ? 0 : right.moreThan(val);
        }

        private void update() {
            height = 1;
            cnt = ownCnt;

            if (right != null) {
                height = Math.max(height, right.height + 1);
                cnt += right.cnt;
            }

            if (left != null) {
                height = Math.max(height, left.height + 1);
                cnt += left.cnt;
            }
        }

        private void print(int margin) {
            for (int i = 0; i < margin; i++)
                System.out.print(" ");
            System.out.println(val + " {" + ownCnt + "} {" + cnt + "}");

            if (left != null)
                left.print(margin + 2);

            if (right != null)
                right.print(margin + 2);
        }
    }
}
