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 Main {
	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();}
        int nextInt(){ return Integer.parseInt(next());}long nextLong(){return Long.parseLong(next());}double nextDouble(){return Double.parseDouble(next());}
        String nextLine(){String str = ""; try{str = br.readLine(); } catch (IOException e) {e.printStackTrace();} return str; } 
    }
	public static void main (String[] args) throws Exception {
		FastReader sc = new FastReader();
        PrintWriter out = new PrintWriter(System.out);
        int t = 1;
//        t = sc.nextInt();
        z : while(t-->0) {
			int n = sc.nextInt();
			int a[] = new int[n];
			Map<Integer,Integer> m = new HashMap<>();
			for(int i=0;i<n;i++) a[i] = sc.nextInt();
			int pre[] = new int[n];
			int suf[] = new int[n];
			for(int i=0;i<n;i++) {
				m.put(a[i], m.getOrDefault(a[i], 0)+1);
				pre[i] = m.get(a[i]);
			}
			m.clear();
			for(int i=n-1;i>=0;i--) {
				m.put(a[i], m.getOrDefault(a[i], 0)+1);
				suf[i] = m.get(a[i]);
			}
			FenTree tre = new FenTree((int)(1e6+10));
			long sum = 0;
			for(int i=n-1;i>=0;i--) {
				sum += tre.query(pre[i]);
				tre.update(suf[i]+1, 1);
			}
			out.write(sum+"\n");
        }
		out.close();
	}
}
class FenTree {
	int n;
	long fen[];
	FenTree(int n){
		this.n = n+1;
		fen = new long[n+1];
	}
	FenTree(int b[]) {
		fen = new long[b.length+1];
		this.n = b.length+1;
		for(int i=1;i<=b.length;i++) {
			update(i,b[i-1]);
		}
	}
	void update(int idx, int val) {
		while(idx<n) {
			fen[idx] += val;
			idx = idx + (idx & -idx);
		}
	}
	long query(int ind) {
		long sum = 0;
	    while(ind > 0) {
	        sum += fen[ind]; 
	        ind = ind - (ind & -ind); 
	    }
	    return sum; 
	}
}
class pair implements Comparable<pair>{
	int a;
	int b; 
	pair(int a,int b){
		this.a = a;
		this.b = b;
	}
	@Override
    public int compareTo(pair p){
        return this.b - p.b;
    }
}