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 segTree {

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = sc.nextInt();
		int[] arr = new int[n];
		for(int i=0;i<n;i++)
			arr[i] = sc.nextInt();
		Pair[] left = calc_left(arr);
		Pair[] right = calc_right(arr);
//		System.out.println(Arrays.toString(left));
//		System.out.println(Arrays.toString(right));
		int size = 1;
		while(size<n)
			size<<=1;
		int[] b = new int[size + 1];
		SegmentTree st = new SegmentTree(b);
		long ans= 0;
		for(int i=0, j=0; i<n;i++) {
			while(j<n && right[j].count<left[i].count) {
				st.update_point(right[j].idx, 1);
				j++;
			}
			ans += st.query(left[i].idx + 1, size);
		}
		out.println(ans);
		out.close();
	}

	private static Pair[] calc_left(int[] arr) {
		HashMap<Integer, Integer> freq = new HashMap<Integer, Integer>();
		int n =arr.length;
		Pair left[] =new Pair[n];
		for(int i=0;i<n;i++) {
			left[i] =  new Pair(freq.getOrDefault(arr[i], 0) + 1, i+1);
			freq.put(arr[i],left[i].count);
		}
		Arrays.parallelSort(left);
		return left;
	}
	private static Pair[] calc_right(int[] arr) {
		HashMap<Integer, Integer> freq = new HashMap<Integer, Integer>();
		int n =arr.length;
		Pair right[] =new Pair[n];
		for(int i=n-1;i>=0;i--) {
			right[i] =  new Pair(freq.getOrDefault(arr[i], 0) + 1, i+1);
			freq.put(arr[i],right[i].count);
		}
		Arrays.parallelSort(right);
		return right;
	}


	static class SegmentTree { // 1-based DS, OOP

		int N; // the number of elements in the array as a power of 2 (i.e. after padding)
		int[] array, sTree, lazy;

		SegmentTree(int[] in) {
			array = in;
			N = in.length - 1;
			sTree = new int[N << 1]; // no. of nodes = 2*N - 1, we add one to cross out index zero
//			lazy = new int[N << 1];
//			build(1, 1, N);
		}

		void build(int node, int b, int e) // O(n)
		{
			if (b == e)
				sTree[node] = array[b];
			else {
				int mid = b + e >> 1;
				build(node << 1, b, mid);
				build(node << 1 | 1, mid + 1, e);
				sTree[node] = sTree[node << 1] + sTree[node << 1 | 1];
			}
		}

		void update_point(int index, int val) // O(log n)
		{
			index += N - 1;
			sTree[index] += val;
			while (index > 1) {
				index >>= 1;
				sTree[index] = sTree[index << 1] + sTree[index << 1 | 1];
			}
		}

		void update_range(int i, int j) // O(log n)
		{
			update_range(1, 1, N, i, j);
		}

		void update_range(int node, int b, int e, int i, int j) {
			if (i > e || j < b)
				return;
			if (b >= i && e <= j) {
				sTree[node] = (e - b + 1) - sTree[node];
				lazy[node] += 1;
			} else {
				int mid = b + e >> 1;
				propagate(node, b, mid, e);
				update_range(node << 1, b, mid, i, j);
				update_range(node << 1 | 1, mid + 1, e, i, j);
				sTree[node] = sTree[node << 1] + sTree[node << 1 | 1];
			}

		}

		void propagate(int node, int b, int mid, int e) {
			lazy[node << 1] += lazy[node];
			lazy[node << 1 | 1] += lazy[node];
			if (lazy[node] % 2 == 1) {
				sTree[node << 1] = (mid - b + 1) - sTree[node << 1];
				sTree[node << 1 | 1] = (e - mid) - sTree[node << 1 | 1];
			}
			lazy[node] = 0;
		}

		int query(int i, int j) {
			return query(1, 1, N, i, j);
		}

		int query(int node, int b, int e, int i, int j) // O(log n)
		{
			if (i > e || j < b)
				return 0;
			if (b >= i && e <= j)
				return sTree[node];
			int mid = b + e >> 1;
//			propagate(node, b, mid, e);
			int q1 = query(node << 1, b, mid, i, j);
			int q2 = query(node << 1 | 1, mid + 1, e, i, j);
			return q1 + q2;

		}
	}
	static class Pair implements Comparable<Pair>{
		int count;
		int idx;
		public Pair(int vol, int idx) {
			this.count = vol;
			this.idx = idx;
		}
		@Override
		public int compareTo(Pair o) {
			int res = count- o.count;
			if(res == 0)
				return o.idx - idx;
			return res;
		}
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

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

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

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

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public boolean ready() throws IOException {
			return br.ready();
		}
	}
}

    	 	 	 		 	 	 		   				  		