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.util.*;
import java.io.*;
public class Main {
public static void update(int bit[],int a,int val){
    int n=bit.length;	
	while(a < n){
		bit[a]+=val;
		a+=a&(-a);
	}
}
public static int query(int bit[],int a){
     int sum=0;
     while(a > 0){
     	sum+=bit[a];
     	a-=a&(-a);
     }
     return sum;
}	
public static void main(String [] args)throws Exception{
	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	int n=Integer.parseInt(br.readLine());
	StringTokenizer st=new StringTokenizer(br.readLine());
	int array[]=new int[n];
	for(int i=0;i<n;i++)
		array[i]=Integer.parseInt(st.nextToken());
	int temp[]=array.clone();
	Arrays.sort(temp);
	for(int i=0;i<n;i++){
		array[i]=Arrays.binarySearch(temp,array[i])+1;
	}
	HashMap<Integer,Integer>map=new HashMap<Integer,Integer>();
	temp=new int[n];
	for(int i=0;i<n;i++){
		if(map.containsKey(array[i])){
			temp[i]=map.get(array[i])+1;
			map.put(array[i],temp[i]);
		}
		else{
			map.put(array[i],1);
			temp[i]=1;
		}
	}
	//for(int i=0;i<n;i++)System.out.print(temp[i]+" ");
	//System.out.println();
	int bit[]=new int[n+1];
	long ans=0;
	map=new HashMap<Integer,Integer>();
	for(int i=n-1;i>=0;i--){
	   ans+=query(bit,temp[i]-1);
	   if(map.containsKey(array[i])){
	   	map.put(array[i],map.get(array[i])+1);
	   }
	   else{
	   	map.put(array[i],1);
	   }
	   update(bit,map.get(array[i]),1);	
	}
	System.out.println(ans);
}
}	
	
   