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

public class Main
{
	public static void main(String[] args) {
		new Thread(null, new Runnable() {
			public void run() {
                solve();
            }
        }, "1", 1 << 26).start();
	}
	static void solve () {
		FastReader fr =new FastReader();	PrintWriter op =new PrintWriter(System.out);
 
 		int n =fr.nextInt() ,a[] =new int[n] ,frnt[] =new int[n] ,bck[] =new int[n] ,i ;	long bit[] =new long[n+1] ,ans =0l ;
 		for (i =0 ; i<n ; ++i)	a[i] =fr.nextInt() ;	HashMap<Integer,Integer> ct =new HashMap<>() ;
 		for (i =0 ; i<n ; ++i) {
 			if (!ct.containsKey(a[i]))	ct.put(a[i],0) ;	ct.put(a[i],ct.get(a[i])+1) ;
 			frnt[i] =ct.get(a[i]) ;
 		}
 		ct.clear() ;
 		for (i =n-1 ; i>-1 ; --i) {
 			if (!ct.containsKey(a[i]))	ct.put(a[i],0) ;	ct.put(a[i],ct.get(a[i])+1) ;
 			bck[i] =ct.get(a[i]) ;
 		}
 		for (i =n-1 ; i>-1 ; --i)	upd (bit , bck[i] , 1l) ;
 		for (i =0 ; i<n ; ++i) {
 			upd (bit , bck[i] , -1) ;	ans += query (bit , frnt[i]-1) ;
 		}
		op.println(ans) ;	op.flush();	op.close();
	}
	static void upd (long bit[] , int pos , long v) {
		for (int i =pos ; i<bit.length ; i += (i&(-i)))	bit[i] += v ;
	}
	static long query (long bit[] , int pos) {
		long r =0l ;
		for (int i =pos ; i>0 ; i -= (i&(-i)))	r += bit[i] ;
		return r;
	}
	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br =new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st==null || (!st.hasMoreElements())) 
			{
				try
				{
					st =new StringTokenizer(br.readLine());
				}
				catch(IOException e)
				{
					e.printStackTrace();
				}
				
			}
			return st.nextToken();
		}

		String nextLine() {
			String str ="";

			try
			{
				str =br.readLine();
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}

			return str;
		}

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

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