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.math.*;
import java.io.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;

public class Main{
	
	// ArrayList<Integer> lis = new ArrayList<Integer>();
	// ArrayList<String> lis = new ArrayList<String>();
	//  PriorityQueue<P> que = new PriorityQueue<P>();
	// PriorityQueue<Integer> que = new PriorityQueue<Integer>();
	//  Stack<Integer> que = new Stack<Integer>();
    //	static long sum=0;
	// 1000000007 (10^9+7)
	static int mod = 1000000007;
	//static int mod = 1000000009,r=0; ArrayList<Integer> l[]= new ArrayList[n];
   // static int dx[]={1,-1,0,0};
//	static int dy[]={0,0,1,-1};
//	static int dx[]={1,-1,0,0,1,1,-1,-1};
//  static int dy[]={0,0,1,-1,1,-1,1,-1};
	//static Set<Integer> set = new HashSet<Integer>();
	
	
public  static void main(String[] args)   throws Exception, IOException{
   //String line=""; throws Exception, IOException
   //(line=br.readLine())!=null
	//Scanner sc =new Scanner(System.in);
	// !!caution!! int long //  
	Reader sc = new Reader(System.in);
//	int n=0;

 //,a=sc.nextInt(),b=sc.nextInt();
 	// int a=sc.nextInt(),b=sc.nextInt();
	int n=sc.nextInt();
	//int x=sc.nextInt(),y=sc.nextInt(),xx=sc.nextInt(),yy=sc.nextInt();
	int x[]=new int[n];
	int y[]=new int[n];
	int z[]=new int[n];
    
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
  
    
	 for(int i=0;i<n;i++){
	   int a=sc.nextInt();  x[i]=a; 
	   if( map.containsKey(a) ){int e=map.get(a);   map.put(a,  e+1 ); y[i]=e+1; }
	   else { map.put(a,  1 ); y[i]=1;}  
	   
	 }
	 map.clear();
	 
	 for(int i=0;i<n;i++){
		   int a=x[n-i-1]; 
		   if( map.containsKey(a) ){int e=map.get(a);   map.put(a,  e+1 ); z[n-i-1]=e+1; }
		   else { map.put(a,  1 ); z[n-i-1]=1;}  
		   
		 }
	 
if(n==1){System.out.println(0); return;}
	 bit bt=new bit(n); 

	 long r=0;
	 bt.add(1, 1);
	 for(int i=n-2;-1<i;i--){
		
		 r+=bt.sum( y[i]-1 );
		 
		 bt.add(z[i], 1);
		 
		 
	 }

	  
	System.out.println(r);

}


static void db(Object... os){
    System.err.println(Arrays.deepToString(os));

}

}

class bit { //* 1 based  possiblity long!
	 int b[],n;
	 bit(int n){
		 this.n=n;
		 b=new int[n+1];
	 }
	int sum(int i){
		int s=0;
		while(i>0){
			s+=b[i];
			i -= i&-i;
		}
	return s;	
	}
	void add(int i,int x){
		while(i<=n){
			b[i]+=x;
			i += i&-i;
		}	
	}
}

  
   class P implements Comparable<P>{
//		implements Comparable<Pair>
		int id; long d;
		P(int id,long d){
			this.id=id;
			this.d=d;
		} 
		public int compareTo(P x){
		   	 return  0<=d-x.d?1:-1 ;  //ascend
		   }
	 
 }


/*
class Pair implements Comparable<Pair>{
//	implements Comparable<Pair>
	int a,b;
	Pair(int a,int b){
		this.a=a;
		this.b=b;
	} 
	public int compareTo(Pair x){
	   	 return a-x.a; //descend
	   }
}*/

class Reader
{ 
	private BufferedReader x;
	private StringTokenizer st;
	
	public Reader(InputStream in)
	{
		x = new BufferedReader(new InputStreamReader(in));
		st = null;
	}
	public String nextString() throws IOException
	{
		while( st==null || !st.hasMoreTokens() )
			st = new StringTokenizer(x.readLine());
		return st.nextToken();
	}
	public int nextInt() throws IOException
	{
		return Integer.parseInt(nextString());
	}
	public long nextLong() throws IOException
	{
		return Long.parseLong(nextString());
	}
	public double nextDouble() throws IOException
	{
		return Double.parseDouble(nextString());
	}
}