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.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class A {

	public static void main(String[] args) throws NumberFormatException, IOException {
		Scanner sc = new Scanner();
		PrintWriter out = new PrintWriter(System.out);
		int n = sc.nextInt();
		int [] a = new int[n];
		HashMap<Integer, Integer> pre = new HashMap<>();
		HashMap<Integer, Integer> suff = new HashMap<>();
		for (int i = 0; i < a.length; ++i) {
			a[i] = sc.nextInt();
			pre.put(a[i], pre.getOrDefault(a[i], 0)+1);
		}
		long ans = 0;
		FenwickTree ft = new FenwickTree(n);
		for (int i = n-1; i >= 0; --i) {
			int f = pre.getOrDefault(a[i], 0);
			ans += ft.rsq(f);
			if(f > 1)
				pre.put(a[i], f-1);
			else pre.remove(a[i]);
			suff.put(a[i], suff.getOrDefault(a[i], 0)+1);
			ft.point_update(suff.getOrDefault(a[i], 0)+1, 1);
		}
		out.println(ans);
		out.flush();
		out.close();
	}
	static class FenwickTree { // one-based DS

		int n;
		int[] ft;

		FenwickTree(int size) { n = size; ft = new int[n+1]; }

		int rsq(int b) //O(log n)
		{
			int sum = 0;
			while(b > 0) { sum += ft[b]; b -= b & -b;}		//min?
			return sum;
		}

		void point_update(int k, int val)	//O(log n), update = increment
		{
			while(k <= n) { ft[k] += val; k += k & -k; }		//min?
		}
	}
	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		public Scanner(String file) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(file));

		}
		public Scanner(){
			br = new BufferedReader(new InputStreamReader(System.in));
		}

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

		public int nextInt() throws NumberFormatException, IOException {
			return Integer.parseInt(next());
		}
		public long nextLong() throws NumberFormatException, IOException {
			return Long.parseLong(next());
		}
	}
}