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

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

class TaskD {
    static int[] A;

    static void add(int ind, int val, int a,int b, int n){

        if(ind<a || ind>b) return;
        if(a==b) {
            A[n] += val;
            return;
        }
        int m = (a+b)/2;
        add(ind,val,a,m,2*n);
        add(ind,val,m+1,b,2*n+1);
        A[n]= A[2*n] + A[2*n+1];
    }
    static int query(int l, int r, int a, int b, int n){

        if(r<a || l>b) return 0;
        if(l<=a && b<= r) return A[n];
        int m = (a+b)/2;
        int sum1= query(l,r,a,m,2*n);
        int sum2= query(l,r,m+1,b,2*n+1);
        return sum1+sum2;
    }

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n= in.nextInt();
        int[] ar= new int[n];
        for(int i=0; i<n; i++){
            ar[i]=in.nextInt();
        }
        HashMap<Integer, Integer> repLeft = new HashMap();
        HashMap<Integer, Integer> repRight= new HashMap();
        long answer=0;
        int[] left= new int[n];
        int[] right= new int[n];
        for(int i=0; i<n; i++){
            if(repLeft.containsKey(ar[i])){
                repLeft.put(ar[i], repLeft.get(ar[i])+1);
            }else{
                repLeft.put(ar[i],1);
            }
            left[i]= repLeft.get(ar[i]);
        }
        for(int j=n-1; j>=0; j--){
            if(repRight.containsKey(ar[j])){
                repRight.put(ar[j], repRight.get(ar[j])+1);
            }else{
                repRight.put(ar[j],1);
            }
            right[j]= repRight.get(ar[j]);
        }
        A= new int[4*n];

for(int i=n-1; i>=0; i--){
            int c= left[i];
            answer += query(1,c-1, 1,n, 1);
            add( right[i],1,1,n,1);
        }
        out.println(answer);
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

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

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

}

