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

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        JPashmakAndParmidasProblem solver = new JPashmakAndParmidasProblem();
        solver.solve(1, in, out);
        out.close();
    }

    static class JPashmakAndParmidasProblem {
        int n;
        int[] arr;
        HashMap<Integer, Integer> count;

        public void readInput(Scanner sc) {
            n = sc.nextInt();
            arr = new int[n];
            for (int i = 0; i < n; i++)
                arr[i] = sc.nextInt();
        }

        public void solve(int testNumber, Scanner sc, PrintWriter pw) {
            readInput(sc);
            count = new HashMap<>();
            int N = 1;
            while (N < n)
                N <<= 1;
            SegmentTree st = new SegmentTree(N);
            for (int i = n - 1; i >= 0; i--) {
                count.put(arr[i], count.getOrDefault(arr[i], 0) + 1);
                st.update_point(count.get(arr[i]), 1);
            }
            long ans = 0;
            HashMap<Integer, Integer> count2 = new HashMap<>();
            for (int i = 0; i < n; i++) {
                st.update_point(count.get(arr[i]), -1);
                count.put(arr[i], count.get(arr[i]) - 1);
                count2.put(arr[i], count2.getOrDefault(arr[i], 0) + 1);
                ans += st.query(1, count2.get(arr[i]) - 1);
            }
            pw.println(ans);
        }

        public class SegmentTree {
            int N;
            int[] sTree;
            int[] lazy;

            SegmentTree(int N) {
                this.N = N;
                sTree = new int[N << 1];        //no. of nodes = 2*N - 1, we add one to cross out index zero
                lazy = new int[N << 1];
            }

            void update_point(int index, int val) {
                index += N - 1;
                sTree[index] += val;
                while (index > 1) {
                    index >>= 1;
                    sTree[index] = sTree[index << 1] + sTree[index << 1 | 1];
                }
            }

            int query(int i, int j) {
                return query(1, 1, N, i, j);
            }

            int query(int node, int b, int e, int i, int j) {
                if (i > e || j < b)
                    return 0;
                if (b >= i && e <= j)
                    return sTree[node];
                int mid = b + e >> 1;
                int q1 = query(node << 1, b, mid, i, j);
                int q2 = query(node << 1 | 1, mid + 1, e, i, j);
                return q1 + q2;

            }

        }

    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

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

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

    }
}


   	  		 	  			 		  	 	 		 	  	