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.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.TreeMap;


public class D {

    static StringTokenizer st;
    static BufferedReader br;
    static PrintWriter pw;
    static class Sort implements Comparable<Sort> {
        int val, ind;
        public int compareTo(Sort arg0) {
            return this.val-arg0.val;
        }
    }
    static int []t;
    static int max_n = (int) 1e6;
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int n = nextInt();
        Sort[]a = new Sort[n+1];
        for (int i = 1; i <= n; i++) {
            a[i] = new Sort();
            a[i].val = nextInt();
            a[i].ind = i;
        }
        Arrays.sort(a, 1, n+1);
        int[]b = new int[n+1];
        int s = 1;
        b[a[1].ind] = s;
        for (int i = 2; i <= n; i++) {
            if (a[i].val != a[i-1].val)
                s++;
            b[a[i].ind] = s;
        }
        int[]cnt = new int[max_n+1];
        t = new int[max_n+1];
        int[]c = new int[n+1];
        for (int i = n; i >= 1; i--) {
            c[i] = ++cnt[b[i]];
            inc(c[i], 1);
        }
        long ans = 0;
        Arrays.fill(cnt, 0);
        for (int i = 1; i <= n; i++) {
            int q = ++cnt[b[i]];
            inc(c[i], -1);
            ans += sum(q-1);
        }
        System.out.println(ans);
        pw.close();
    }
    private static int sum(int ind) {
        int res = 0;
        for (int i = ind; i >= 1; i = (i & (i-1))) {
            res += t[i];
        }
        return res;
    }
    private static void inc(int ind, int delta) {
        for (int i = ind; i <= max_n; i = (i | (i-1))+1) {
            t[i] += delta;
        }
    }
    private static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    private static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
    private static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }
    private static String next() throws IOException {
        while (st==null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }
}
