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.awt.*;
import java.util.*;

public class D459 {
	
	public void input() {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		for (int i = 0; i < n; i++) a[i] = sc.nextInt();
	}
	
	public void sort(int arr[], int pos[], int left, int right) {
		if (left >= right) return;
		int x, i = left, j = right, tmp;
		x = arr[(left + right)/2];
		do {
			while (arr[i] < x) i++;
			while (x < arr[j]) j--;
			if (i <= j) {
				tmp = arr[i];
				arr[i] = arr[j];
				arr[j] = tmp;
				tmp = pos[i];
				pos[i] = pos[j];
				pos[j] = tmp;
				i++;
				j--;
			}
		} while (i <= j);
		sort(arr, pos, left, j);
		sort(arr, pos, i, right);
	}

	public void roiRac() {
		int[] pos = new int[MAXN];
		for (int i = 0; i < n; i++) pos[i] = i;
		sort(a, pos, 0, n - 1);
		int j = 1;
		b[pos[0]]= j;
		for (int i = 1; i < n; i++) {
			if (a[i] != a[i - 1]) j++;
			b[pos[i]] = j;
		}
	}
	
	public void preCalc() {
		int[] mark = new int[MAXN];
		Arrays.fill(mark, 0);
		for (int i = 0; i < n; i++) {
			mark[b[i]]++;
			l[i] = mark[b[i]];
		}
		Arrays.fill(mark, 0);
		for (int i = n - 1; i >= 0; i--) {
			mark[b[i]]++;
			r[i] = mark[b[i]];
		}
	}
	
	public long get(int i) {
		long kq = 0;
		while (i > 0) {
			kq += bit[i];
			i = i - (i & (-i));
		}
		return kq;
	}
	
	public void update(int i, int gt) {
		while (i <= n) {
			bit[i] += gt;
			i = i + (i & (-i));
		}
	}
	
	public void solve() {
		long res = 0;
		for (int i = n - 1; i >= 0; i--) {
			res += get(l[i]);
			update(r[i] + 1, 1);
		}
		System.out.println(res);
	}
	
	public static void main(String ds[]) {
		D459 obj = new D459();
		obj.input();
		obj.roiRac();
		obj.preCalc();
		obj.solve();
	}
	
	private int n;
	private long[] bit = new long[MAXN];
	private int[] a = new int[MAXN];
	private int[] b = new int[MAXN];
	private int[] l = new int[MAXN];
	private int[] r = new int[MAXN];
	public static int MAXN = 1000005;
}