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 implements Runnable {
    
        class Fenwick{
            int[] f;
            int N;
            Fenwick(int n){
                N = n;
                f = new int[N];
            }
            
            public int get(int x){
                int value = 0;
                for(; x >= 0; x = (x & (x+1)) - 1 ) value += f[x];
                return value;
            }
            
            public void set(int x){
                for(; x < N; x = x | (x+1)) f[x] += 1;
            }
            
        }
        
        
    
	public void solve() throws IOException {
		int N = nextInt();
                int[]  a = new int[N];
                for(int i = 0; i < N; i++) a[i] = nextInt();
                
                int[] leftCnt = new int[N]; // the cnts of same number to its left
                int[] rightCnt = new int[N]; //the cnts of same number to its right
                
                Map<Integer, Integer> cnts = new HashMap<>();
                for(int i = 0; i < N; i++){
                    Integer curCnt = cnts.get(a[i]);
                    if(curCnt == null)curCnt = 0;
                    leftCnt[i] = curCnt;
                    curCnt++;
                    cnts.put(a[i], curCnt);
                }
                
                cnts.clear();
                for(int i = N-1; i >= 0; i--){
                    Integer curCnts = cnts.get(a[i]);
                    if(curCnts == null) curCnts = 0;
                    rightCnt[i] = curCnts;
                    curCnts++;
                    cnts.put(a[i], curCnts);
                }
                
                Fenwick bit = new Fenwick(N+10);//will keep the rightcnts COUNT
                
                long answer = 0;
                for(int i = N-1; i >= 0; i--){
                    //Hello, I am a[i] and i got leftCnt[i] same numbers left of me
                    answer += bit.get(leftCnt[i] - 1); //So, how many have distinct numbers have cnts less than me, to the right
                    bit.set(rightCnt[i]); //NOTE: we are updating only RIGHTcnts
                }
                
                System.out.println(answer);
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	//-----------------------------------------------------------
	public static void main(String[] args) {
		new Main().run();
	}

        public void print1Int(int[] a){
                for(int i = 0; i < a.length; i++)
                        System.out.print(a[i] + " ");
                System.out.println();
        }
        
        public void print2Int(int[][] a){
                for(int i = 0; i < a.length; i++){
                        for(int j = 0; j < a[0].length; j++){
                                System.out.print(a[i][j] + " ");
                        }
                        System.out.println();
                }
        }
        
	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			tok = null;
			solve();
			in.close();
		} catch (IOException e) {
			System.exit(0);
		}
	}

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

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

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

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	BufferedReader in;
	StringTokenizer tok;
}