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.HashMap;
import java.util.Map;
import java.util.Scanner;


// author: Prajogo Tio  

public class Pashmak {
	
	// at index i, 
	static int l[];
	static int r[];
	
	static int low[];
	static int high[];

	static int count[];

	public static void main(String[] args) {
	
		Scanner in = new Scanner(System.in);
		
		int n = in.nextInt();
		
		l = new int[n];
		r = new int[n];
		
		low = new int[1000001 * 4];
		high = new int[1000001 * 4];
		count = new int[1000001 * 4];

		int[] rel = new int[n];
		
		for(int i=0; i<n; i++) {
			rel[i] = in.nextInt();
		}
		
		
		// number / freq
		Map<Integer, Integer> map = new HashMap<>();
		
		for(int i=0; i<n; i++) {
			int cur = rel[i];
			if(!map.containsKey(cur)) {
				map.put(cur, 1);
			}
			else {
				map.put(cur, map.get(cur) + 1);
			}
			
			l[i] = map.get(cur);
		}
		
		map.clear();
		
		for(int i=n-1; i>=0; i--) {
			int cur = rel[i];
			if(!map.containsKey(cur)) {
				map.put(cur, 1);
			}
			else {
				map.put(cur, map.get(cur) + 1);
			}
			
			r[i] = map.get(cur);
		}
		
		
		init(1, 0, n-1);
		
		long res = 0;
		
		for(int i=n-1; i>=1; i--) {	
			update(1, r[i], r[i]);
			
			// we need atleast 1 occurence
			res += find(1, 1,l[i-1]-1);
		}
		
		System.out.println(res);
	}



	private static int find(int i, int l, int r) {
		if(low[i] > r || high[i] < l) {
			return 0;
		}
		else if(low[i] >= l && high[i] <= r) {
			return count[i];
		} 
		
		return find(2*i, l, r) + find(2*i+1, l, r);
	}



	private static void update(int i, int l, int r) {
		if(low[i] > r || high[i] < l) {
			return;
		}
		else if(low[i] == l && high[i] == r) {
			count[i]++;
			return;
		}
		
		update(2*i, l, r);
		update(2*i + 1, l, r);
		
		count[i] = count[2*i] + count[2*i + 1];
	}



	private static void init(int i, int l, int r) {
		low[i] = l;
		high[i] = r;
		
		if(l == r) {
			return;
		}
		
		int mid = (l + r)/2;
		
		init(2*i, l, mid);
		init(2*i + 1, mid+1, r);
	}

}
