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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

/*
 * Target : Tshirt in a major programming competition.
 */
public class Main implements Runnable {

	int mod = 998244353;
	int mod2 = mod;
	int sz[];
	int id[];
	int tree[];

	int brute(int n, int a[]) throws IOException {
//		int n = nextInt();
////	f(l, r, x) = no of occurrences of x in a[l], a[l + 1], .. a[r]
//		// find f(1, i, a[i]) > f(j, n, a[j])
//		int a[] = new int[n];
//		for (int i = 0; i < n; ++i) {
//			a[i] = nextInt();
//		}
		int ans = 0;
		for (int i = 0; i < n; ++i) {
			for (int j = i + 1; j < n; ++j) {
				int cnt1 = 0, cnt2 = 0;
				for (int k = 0; k <= i; ++k)
					if (a[k] == a[i])
						cnt1++;
				for (int k = j; k < n; ++k)
					if (a[k] == a[j])
						cnt2++;
				if (cnt1 > cnt2)
					ans++;
			}
		}
		return ans;
	}

	void test() throws IOException {
//		Random rnd = new Random();
//		while (true) {
//			int n = rnd.nextInt(2) + 1;
//			int a[] = new int[n];
//			for (int i = 0; i < n; ++i)
//				a[i] = rnd.nextInt((int) 1e9) + 1;
//			int ans1 = brute(n, a);
//			int ans2 = solve(n, a);
//			System.err.println("testing");
//			if (ans1 != ans2) {
//				pw.println(n);
//				for (int i = 0; i < n; ++i) {
//					pw.print(a[i] + " ");
//				}
//				return;
//			}
//		}
		int n = nextInt();
		int a[] = new int[n];
		for (int i = 0; i < n; ++i)
			a[i] = nextInt();
		pw.println((long) solve(n, a));
	}

	private long solve(int n, int a[]) throws IOException {
//		int n = nextInt();
////		f(l, r, x) = no of occurrences of x in a[l], a[l + 1], .. a[r]
//		// find f(1, i, a[i]) > f(j, n, a[j])
//		int a[] = new int[n];
//		tree = new int[n + 1];
//		for (int i = 0; i < n; ++i) {
//			a[i] = nextInt();
//		}
//		System.err.println(Arrays.toString(a));
		tree = new int[n + 1];
		Map<Integer, Integer> cnt = new HashMap<>();
		for (int i = n - 1; i >= 0; --i) {
			if (cnt.containsKey(a[i])) {
				cnt.put(a[i], cnt.get(a[i]) + 1);
			} else {
				cnt.put(a[i], 1);
			}
			add(cnt.get(a[i]), 1);
		}
//		System.err.println(cnt);
		Map<Integer, Integer> cnt1 = new HashMap<>();
		long ans = 0;
		for (int i = 0; i < n; ++i) {
//			System.err.println(cnt);
//			System.err.println(cnt1);
			add(cnt.get(a[i]), -1);
			cnt.put(a[i], cnt.get(a[i]) - 1);
			if (cnt1.containsKey(a[i])) {
				cnt1.put(a[i], cnt1.get(a[i]) + 1);
			} else {
				cnt1.put(a[i], 1);
			}
			int tmp = cnt1.get(a[i]);
			ans += sum(tmp - 1);
		}
		return ans;
	}

	void add(int p, int x) {
		int n = tree.length - 1;
		for (int i = p; i <= n; i += i & -i) {
			tree[i] += x;
		}
	}

	long sum(int p) {
		long sum = 0;
		for (int i = p; i >= 1; i -= i & -i) {
			sum += tree[i];
		}
		return sum;
	}

	BufferedReader br;
	StringTokenizer st;
	PrintWriter pw;

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

	public void run() {
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			pw = new PrintWriter(System.out);
			st = null;
			test();
			pw.flush();
			pw.close();
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

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

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

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

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