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 Main {

    public static void main(String[] args) throws IOException{
        Reader.init(System.in);
        int n = Reader.nextInt();
        int[] a = new int[n];
        int[] pref = new int[n];
        int[] suff = new int[n];
        Map<Integer, Integer> occurrences = new HashMap<>();
        for (int i = 0; i < n; i++) {
            a[i] = Reader.nextInt();
            if (!occurrences.containsKey(a[i]))occurrences.put(a[i], 1);
            else occurrences.put(a[i], occurrences.get(a[i]) + 1);
            pref[i] = occurrences.get(a[i]);
        }
        occurrences = new HashMap<>();
        int maxNum = 0;
        for (int i = n - 1; i >= 0; i--) {
            if (!occurrences.containsKey(a[i]))occurrences.put(a[i], 1);
            else occurrences.put(a[i], occurrences.get(a[i]) + 1);
            suff[i] = occurrences.get(a[i]);
            if (suff[i] > maxNum) maxNum = suff[i];
        }

        int[] nextGreater = new int[maxNum + 2];
        for (int aSuff : suff) nextGreater[aSuff]++;
        Fenwick fen = new Fenwick(nextGreater);
        long ans = 0;
//        System.out.println(fen.rangeSum(2, 2));
        for (int i = 0; i < pref.length; i++){
            fen.update(suff[i] + 1, -1);
            ans += fen.sum(pref[i]);
        }
        System.out.println(ans);
    }
}

class Fenwick{
    private long[] tree; //1-indexed.

    Fenwick(int[] arr){
        tree = new long[arr.length + 1];
        for (int i = 0; i < arr.length; i++) {
            update(i + 1, arr[i]);
        }
    }

    long sum(int k){
        long s = 0;
        while (k >= 1){
            s += tree[k];
            k -= k & -k;
        }
        return s;
    }

    //Add x to arr[a].
    void update(int k, int x){
        while (k < tree.length){
            tree[k] += (long) x;
            k += k & -k;
        }
    }
}

/**
 * Reader class based on the article at "https://www.cpe.ku.ac.th/~jim/java-io.html"
 * */
class Reader{
    private static BufferedReader reader;
    private static StringTokenizer tokenizer;
    static void init(InputStream inputStream){
        reader = new BufferedReader(new InputStreamReader(inputStream));
        tokenizer = new StringTokenizer("");
    }
    private static String next() throws IOException {
        String read;
        while (!tokenizer.hasMoreTokens()){
            read = reader.readLine();
            if (read == null || read.equals(""))
                return "-1";
            tokenizer = new StringTokenizer(read);
        }

        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException{
        return Integer.parseInt(next());
    }

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

    //Get a whole line.
//    static String line() throws IOException{
//        return reader.readLine();
//    }
//
//    static double nextDouble() throws IOException{return Double.parseDouble(next());}
}


