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.*;
import java.util.*;

public class D {
    public static void main (String[] args) {
        FastScanner fs = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);

        int n = fs.nextInt();
        int[] a = fs.nextIntArray(n);
        int[] freq = new int[n];
        HashMap<Integer, Integer> fr = new HashMap<>();

        for(int i = 0; i < n; i++) {
            if(!fr.containsKey(a[i])) fr.put(a[i], 0);
            fr.put(a[i], fr.get(a[i])+1);
            freq[i] = fr.get(a[i]);
        }

        FenwickTree ft = new FenwickTree(n);
        fr.clear();
        long res = 0;
        for(int i = n-1; i >= 0; i--) {
            if(freq[i] > 1) {
                long value = ft.rsq(1, freq[i]-1);
                res += value;
            }

            if(!fr.containsKey(a[i])) fr.put(a[i], 0);
            int nf = fr.get(a[i]) + 1;
            fr.put(a[i], nf);
            ft.adjust(nf, 1);
        }

        out.println(res);

        out.close();
    }

    static class FenwickTree {
        int[] tree;
        int n;

        FenwickTree(int n) {
            tree = new int[n+1];
            this.n = n;
        }

        int LSOne (int a) {
            return a & (-a);
        }

        int rsq (int b) {
            int sum = 0;
            for(; b > 0; b -= LSOne(b)) {
                sum += tree[b];
            }
            return sum;
        }

        int rsq (int a, int b) {
            return rsq(b) - (a == 1 ? 0 : rsq(a-1));
        }

        void adjust (int pos, int value) {
            for(; pos <= n; pos += LSOne(pos)) {
                tree[pos] += value;
            }
        }
    }

    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            try	{
                br = new BufferedReader(new InputStreamReader(System.in));
//                br = new BufferedReader(new FileReader("testdata.out"));
                st = new StringTokenizer("");
            } catch (Exception e){e.printStackTrace();}
        }

        public String next() {
            if (st.hasMoreTokens())	return st.nextToken();
            try {st = new StringTokenizer(br.readLine());}
            catch (Exception e) {e.printStackTrace();}
            return st.nextToken();
        }

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

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

        public double nextDouble() {return Double.parseDouble(next());}

        public String nextLine() {
            String line = "";
            try {line = br.readLine();}
            catch (Exception e) {e.printStackTrace();}
            return line;
        }

        public Integer[] nextIntegerArray(int n) {
            Integer[] a = new Integer[n];
            for(int i = 0; i < n; i++) a[i] = nextInt();
            return a;
        }

        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for(int i = 0; i < n; i++) a[i] = nextInt();
            return a;
        }

        public char[] nextCharArray() {
            return nextLine().toCharArray();
        }
    }

}
