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

	public static void main(String[] args) throws Exception{
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		
		int [] a = new int [n+1];
		DataCompress data = new DataCompress();
		
		SegmentTree st = new SegmentTree((int)1e6+1);
		
		HashMap<Integer ,Integer> map = new HashMap<>();
		
		int [] prefix , suffix ; 
		
		prefix = new int [n+1];
		suffix = new int [n+1];
		
		for(int i = 1 ; i <= n ; i++) {
			a[i] = sc.nextInt();
			
			map.put(data.get(a[i]), map.getOrDefault(data.get(a[i]), 0) + 1) ;
			prefix[i] = map.get(data.get(a[i]));
			st.updatePoint(prefix[i], 1);
			
		}
		
		map = new HashMap<>();
		
		for(int i = n ; i > 0 ; i--)
		{
			map.put(data.get(a[i]), map.getOrDefault(data.get(a[i]), 0) + 1) ;
			suffix[i] = map.get(data.get(a[i]));
		}
		
		long ans = 0 ;
		
		for(int i = n ; i > 0 ; i --)
		{
			
			st.updatePoint(prefix[i], -1);
			ans += st.query(suffix[i] , (int)1e6+1);
			
		}
		
		
		System.out.println(ans);
		
		
		
		
		
		
	}
	
	static class DataCompress 
	{
		HashMap<Integer,Integer> map = new HashMap<>();
		int idx = 1 ; 
		
		void assign(int val)
		{
			map.put(val, idx++);
		}
		
		int get (int val)
		{
			if(map.get(val) == null) 
				assign(val);
			
			return map.get(val);
		}
		
		
		@Override
		public String toString() {
			return map+"";
		}
		
	}
	
	static class SegmentTree
	{
		int N = 1;
		int [] sTree  , lazy ; 
		
		SegmentTree (int n)
		{
			N = 1 ; 
			
			while(N < n) N <<= 1 ; 

			N -- ;
	
			sTree = new int [N << 1];
			lazy = new int [N << 1] ;
			
		}
		void updatePoint(int idx , int val )
		{
			idx += N - 1 ; 
			sTree[idx] += val  ;
			
			while(idx > 1)
			{
				idx >>=  1 ;
				sTree[idx] = sTree[idx << 1] + sTree[idx << 1 | 1] ;
			}
		}
		
		void updateRange(int i , int j , long val)
		{
			updateRange(1 , 1 , N , i , j , val );
			
		}
		
		void updateRange(int node , int b , int e , int i , int j , long val)
		{
			if(b > j || e < i)
				return;
			
			if(i <= b && e <= j ) {
				 sTree[node] += (e - b + 1) * val;
				 lazy[node] += val ;
				 return ;
			}
			int mid = (b+e) >> 1 ; 
			
			push(node, b , mid, e);
			
			updateRange(node << 1, b, mid, i, j, val);
			updateRange(node << 1 | 1, mid + 1, e, i, j, val);
			
			sTree[node] = sTree[node << 1] + sTree[node << 1 | 1] ;
			
		}
		
		void push(int node , int b , int mid, int e)
		{
			
			lazy[node << 1] += lazy[node];
			lazy[node << 1 | 1] += lazy[node];
			
			sTree[node << 1] += (mid - b + 1 ) * lazy[node];
			sTree[node << 1 | 1] += (e - mid) * lazy[node] ; 
			
			lazy[node] = 0 ; 
		}
		
		long query(int i , int j)
		{
			return query(1 ,  1 , N , i , j );
		}
		long query(int node , int b , int e , int i ,  int j)
		{
			
			if(b > j || e < i)
				return 0 ;
			
			if(i <= b && e <= j)
				return  sTree[node] ;
			
			int mid = (b + e) >> 1 ; 
			
			push(node, b , mid , e);
			
			return  query(node << 1, b, mid, i, j) +  query(node << 1 | 1, mid + 1, e, i, j);
		
		}
 	}
	
	static class FenwickTree
	{
		int [] ft ;
		
		FenwickTree()
		{
			ft = new int [(int) 1e6 + 1];
		
		}
		
		long rsq (int a)
		{
			long sum = 0 ;
			for(int b = a ; b >0  ; b -= (b&-b)) 
				sum += ft[b];
				
			return sum ;
			
		}
		long rsq(int a , int b)
		{
			return rsq(b) - rsq(a-1);
		}
		
		void adjust(int i , long v)
		{
			for(int k = i ; k < ft.length ; k+= (k & -k)) 
				ft[k] +=  v;
			
		}
		
		
		
	}
	
		
	static class Scanner 
	{
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) 
		{
			br = new BufferedReader(new InputStreamReader(s));
		}

		public String next() throws IOException 
		{
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			
			return st.nextToken();
		}

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

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

	}
}
