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.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class Cf459D {
    FastScanner sc;
    int n, mv;
    int[] A, L, C;
    Map<Integer, Integer> M;
    BIT B;

    public static void main(String[] args) {
        Cf459D cf = new Cf459D();
        System.out.println(cf.run());
    }

    private int getValue(int i) {
        if (M.containsKey(i))
            return M.get(i);
        M.put(i, mv);
        return mv++;
    }

    Cf459D() {
        sc = new FastScanner();
        mv = 0;
        n = sc.nextInt();
        A = new int[n];
        L = new int[n];
        C = new int[n];
        M = new TreeMap<Integer, Integer>();
        B = new BIT(n);
        for (int i = 0; i < n; ++i)
            A[i] = getValue(sc.nextInt());
    }

    public long run() {
        long ans = 0;
        for (int i = 0; i < n; ++i)
            L[i] = ++C[A[i]];
        C = new int[n];
        for (int i = n - 1; i >= 0; --i) {
            ans += B.query(L[i]-1);
            B.update(++C[A[i]]);
        }
        return ans;
    }

    public class BIT {
        int[] A;
        BIT(int n) {
            A = new int[n+1];
        }
        public int query(int idx) {
            int ans = 0;
            while (idx > 0) {
                ans += A[idx];
                idx -= idx & -idx;
            }
            return ans;
        }
        public void update(int idx) {
            update(idx, 1);
        }
        public void update(int idx, int val) {
            while (idx <= n) {
                A[idx] += val;
                idx += idx & -idx;
            }
        }
    }

    public class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        FastScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

    }
}
