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.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.StringTokenizer;

public class A {
    private static int[] tree;
    private static int[] count;

    private static void buildTree(int l, int r, int level) {
        if (l == r) {
            tree[level] = count[l];
            return;
        }
        int mid = (l + r) / 2;
        buildTree(l, mid, 2 * level + 1);
        buildTree(mid + 1, r, 2 * level + 2);
        tree[level] = tree[2 * level + 1] + tree[2 * level + 2];
    }

    private static void updateTree(int l, int r, int level, int p) {
        if (p > r || p < l) {
            return;
        }
        tree[level] -= 1;
        if (l != r) {
            int mid = (l + r) / 2;
            updateTree(l, mid, 2 * level + 1, p);
            updateTree(mid + 1, r, 2 * level + 2, p);
        }
    }

    private static long sumTill(int l, int r, int level, int s, int e) {
        if (s <= l && e >= r) {
            return tree[level];
        }
        if (s > r || e < l) {
            return 0;
        }
        int mid = (l + r) / 2;
        long leftSum = sumTill(l, mid, 2 * level + 1, s, e);
        long rightSum = sumTill(mid + 1, r, 2 * level + 2, s, e);
        return leftSum + rightSum;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
        StringTokenizer st;

        int n = Integer.parseInt(br.readLine());
        int[] seq = new int[n];
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < n; i++) {
            seq[i] = Integer.parseInt(st.nextToken());
        }

        if (n == 1) {
            System.out.println(0);
            return;
        }

        count = new int[n + 1];
        int[] countRL = new int[n];
        IntegerMap integerMapRL = new IntegerMap();
        for (int i = n - 1; i >= 0; i--) {
            integerMapRL.insert(seq[i]);
            countRL[i] = integerMapRL.get(seq[i]);
            count[countRL[i]]++;
        }

        int size = 2 * (int) Math.pow(2.0, Math.ceil(Math.log((n + 1) * 1.0) / Math.log(2.0)) * 1.0) - 1;
        tree = new int[size];
        buildTree(0, n, 0);

        long total = 0;
        IntegerMap integerMapLR = new IntegerMap();
        for (int i = 0; i < n; i++) {
            updateTree(0, n, 0, countRL[i]);
            integerMapLR.insert(seq[i]);
            int countLR = integerMapLR.get(seq[i]);
            if (countLR > 1) {
                long temp = sumTill(0, n, 0, 0, countLR - 1);
                total += temp;
            }
            //System.out.println(countLR + ", " + temp);
            //total += temp;
        }
        System.out.println(total);
    }

    private static class IntegerMap extends HashMap<Integer, Integer> {
        void insert(int key) {
            int count = 1;
            if (containsKey(key)) {
                count += get(key);
            }
            put(key, count);
        }
    }
}

		 			 	 	 	 		 			 			  						