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 d2 {
	BufferedReader br;
	StringTokenizer in;
	PrintWriter out;

	static int t[];
	static int l[];

	static void init(int ind, int lt, int rt) {
		if (lt + 1 == rt) {
			t[ind] = l[lt];
		} else {
			init(2 * ind + 1, lt, (lt + rt) / 2);
			init(2 * ind + 2, (lt + rt) / 2, rt);
			t[ind] = t[2 * ind + 1] + t[2 * ind + 2];
		}
	}

	static long get(int ind, int lt, int rt, int a, int b) {
		if (rt <= a || b <= lt) {
			return 0;
		}
		if (a <= lt && rt <= b) {
			return t[ind];
		}
		return get(2 * ind + 1, lt, (lt + rt) / 2, a, b)
				+ get(2 * ind + 2, (lt + rt) / 2, rt, a, b);
	}

	static void upd(int ind, int lt, int rt, int k, int x) {
		if (lt + 1 == rt) {
			t[ind] = x;
			l[k] = x;
		} else {
			if ((lt + rt) / 2 > k) {
				upd(2 * ind + 1, lt, (lt + rt) / 2, k, x);
			} else {
				upd(2 * ind + 2, (lt + rt) / 2, rt, k, x);
			}
			t[ind] = t[2 * ind + 1] + t[2 * ind + 2];
		}
	}

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

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

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

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

	public static void main(String[] args) throws IOException {
		new d2().run();
	}

	public void solve() throws IOException {

		int n = nextInt();

		TreeMap<Integer, Integer> mp = new TreeMap();

		int a[] = new int[n];
		t = new int[4 * n + 10];
		l = new int[n + 1];

		Arrays.fill(l, 0);

		int count = 1;

		for (int i = 0; i < n; i++) {
			int x = nextInt();

			if (mp.containsKey(x)) {
				a[i] = mp.get(x);
			} else {
				a[i] = count;
				mp.put(x, count);
				count++;
			}
		}

		int total[] = new int[n + 1];
		Arrays.fill(total, 0);

		int counter[] = new int[n + 1];

		for (int i = 0; i < n; i++) {
			total[a[i]]++;
			counter[a[i]]++;
		}

		for (int i = 0; i < n; i++) {
			if (counter[a[i]] > 0) {
				for (int j = 1; j <= counter[a[i]]; j++) {
					l[j]++;
				}
				counter[a[i]] = 0;
			}
		}

		init(0, 0, n + 1);

		long ans = 0;

		Arrays.fill(counter, 0);
		
		for (int i = 0; i < n; i++) {
			counter[a[i]]++;
			int cur = counter[a[i]];
			
			int at_right = total[a[i]] - cur;
			upd(0, 0, n + 1, at_right + 1, l[at_right + 1] - 1);

			ans += get(0, 0, n + 1, 0, cur);
		}

		out.println(ans);
	}

	public void run() {
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);

			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
}
