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.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;

public class PashmakAndParmidasProblem {

	static class SegmentTree {

		int N;
		long[] tree;

		SegmentTree(int n) {
			N = n;
			tree = new long[n << 2];
		}

		int left(int p) {
			return p << 1;
		}

		int right(int p) {
			return left(p) + 1;
		}

		void update_point(int i) {
			update_point(1, 0, N - 1, i);
		}

		void update_point(int p, int L, int R, int idx) {
			if (idx > R || idx < L)
				return;
			if (idx == L && idx == R) {
				tree[p]++;
			} else {
				int mid = (L + R) >> 1, left = left(p), right = right(p);
				update_point(left(p), L, mid, idx);
				update_point(right(p), mid + 1, R, idx);
				tree[p] = tree[left] + tree[right];
			}
		}

		long query(int i) {
			return query(1, 0, N - 1, 0, i);
		}

		long query(int p, int L, int R, int i, int j) {
			if (R < i || L > j)
				return 0;
			if (L >= i && R <= j)
				return tree[p];
			int mid = (L + R) >> 1, left = left(p), right = right(p);
			return query(left, L, mid, i, j) + query(right, mid + 1, R, i, j);
		}
	}

	public static void main(String[] args) throws IOException {
		MyScanner sc = new MyScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = sc.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = sc.nextInt();
		HashMap<Integer, Integer> map = new HashMap<>();
		int[] prefix = new int[n];
		for (int i = 0; i < n; i++) {
			Integer x = map.get(a[i]);
			x = x == null ? 0 : x;
			map.put(a[i], x + 1);
			prefix[i] = map.get(a[i]);
		}
		map.clear();
		int[] suffix = new int[n];
		for (int i = n - 1; i >= 0; i--) {
			Integer x = map.get(a[i]);
			x = x == null ? 0 : x;
			map.put(a[i], x + 1);
			suffix[i] = map.get(a[i]);
		}
		SegmentTree st = new SegmentTree(n);
		long ans = 0;
		for (int i = n - 1; i >= 0; i--) {
			ans += st.query(prefix[i] - 1);
			st.update_point(suffix[i]);
		}
		out.println(ans);
		out.close();
	}

	static class MyScanner {

		BufferedReader br;
		StringTokenizer st;

		MyScanner(InputStream is) {
			br = new BufferedReader(new InputStreamReader(is));
		}

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

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

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