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


public class D{
	
	static class Scanner{
		BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer tk;
		public Scanner(){
			rd = new BufferedReader(new InputStreamReader(System.in));
		}
		String next() throws IOException{
			while(tk == null  || !tk.hasMoreTokens())
				tk = new StringTokenizer(rd.readLine());
			return tk.nextToken();
		}
		int nextInt() throws NumberFormatException, IOException{
			return Integer.valueOf(next());
		}
		long nextLong() throws NumberFormatException, IOException{
			return Long.valueOf(next());
		}
		double nextDouble() throws NumberFormatException, IOException{
			return Double.valueOf(next());
		}
	}
	
	public static class FenwickTree {
	    long[] fenwickTree; int tam;
	    
	    public FenwickTree(int t) {
	        fenwickTree = new long[t]; tam = t;
	    }
	    
	    long query(int a, int b) 
	    {
	        if (a == 0) {
	            long sum = 0;
	            for (; b >= 0; b = (b & (b + 1)) - 1)
	              sum += fenwickTree[b];
	            return sum;
	        } 
	        else { return (query(0, b) - query(0, a - 1));}
	    }

	    void increase(int k, long inc) 
	    { for (; k < tam; k |= k + 1) fenwickTree[k] += inc;}
	}
	
	static int N;
	static int[] array;
	static int[] L, R;
	
	public static void main(String args[]) throws NumberFormatException, IOException{
		Scanner sc = new Scanner();
		N = sc.nextInt();
		array = new int[N];
		L = new int[N];
		R = new int[N];
		for(int i = 0; i < N; i++)
			array[i] = sc.nextInt();
		HashMap<Integer, Integer> times = new HashMap<Integer, Integer>();
		for(int i = 0; i < N; i++){
			if (times.containsKey(array[i]))
				times.put(array[i], times.get(array[i]) + 1);
			else
				times.put(array[i], 1);
			L[i] = times.get(array[i]);
		}
		times.clear();
		for(int i = N - 1; i >= 0; i--){
			if (times.containsKey(array[i]))
				times.put(array[i], times.get(array[i]) + 1);
			else
				times.put(array[i], 1);
			R[i] = times.get(array[i]);
		}
		FenwickTree ft = new FenwickTree(N + 10);
		for(int i = 0; i < N; i++)
			ft.increase(R[i], 1);
		long ans = 0;
		for(int i = 0; i < N; i++){
			ft.increase(R[i], -1);
			ans += ft.query(0, L[i] - 1);
		}
		System.out.println(ans);
	}

}
