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.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Scanner;

public class Main {

	public static int N = (int) 1e6;
	
	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		new Task().solve(in, out);
		out.close();
	}

	public static class Bit {
		int a[];

		Bit() {
			a = new int[N + 10];
		}

		public int lowbit(int x) {
			return x & (-x);
		}

		public void Add(int x, int y) {
			while (x <= N) {
				a[x] = a[x] + y;
				x = x + lowbit(x);
			}
		}

		public long sum(int x) {
			long temp = 0;
			while (x >= 1) {
				temp = temp + a[x];
				x = x - lowbit(x);
			}
			return temp;
		}
	}

	public static class Task {
		int n;
		int a[], b[], c[];
		HashMap<Integer, Integer> pre, aft;

		public void solve(Scanner in, PrintWriter out) {
			a = new int[N + 10];
			b = new int[N + 10];
			c = new int[N + 10];
			n = in.nextInt();
			pre = new HashMap<Integer, Integer>();
			aft = new HashMap<Integer, Integer>();
			
			for (int i = 1; i <= n; i++)
				a[i] = in.nextInt();
			
			for (int i = 1; i <= n; i++) {
				int cnt;
				if (pre.containsKey(a[i])) {
					cnt = pre.get(a[i]);
					cnt++;
				} else {
					cnt = 1;
				}
				pre.put(a[i], cnt);
				b[i] = cnt;
			}

			for (int i = n; i >= 1; i--) {
				int cnt;
				if (aft.containsKey(a[i])) {
					cnt = aft.get(a[i]);
					cnt++;
				} else {
					cnt = 1;
				}
				aft.put(a[i], cnt);
				c[i] = cnt;
			}

			Bit mybit = new Bit();
			for (int i = 1; i <= n; i++)
				mybit.Add(c[i], 1);
			long ans = 0;
			for (int i = 1; i <= n; i++) {
				mybit.Add(c[i], -1);
				ans = ans + mybit.sum(b[i] - 1);
			}
			out.println(ans);

		}
	}

}