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.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;

public class D {

	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[] A = new int[n];
		int[] occB = new int[n];
		int[] occF = new int[n];
		
		HashMap<Integer, Integer> map = new HashMap<>();
		
		BIT t = new BIT(n);
		
 		for (int i = 0; i < n; i++) {
			int cur = sc.nextInt();
			A[i] = cur;
			if(!map.containsKey(cur))
				map.put(cur, 0);
			map.put(cur, map.get(cur) + 1);
			occF[i] = map.get(cur);
		}
 		map = new HashMap<>();;
 		for (int i = n - 1; i >= 0; i--) {
			int cur = A[i];
			if(!map.containsKey(cur))
				map.put(cur, 0);
			map.put(cur, map.get(cur) + 1);
			occB[i] = map.get(cur);
			t.update(occB[i], 1);
		}
 		
 		long ans = 0;
 		
 		for (int i = 0; i < n; i++) {
			t.update(occB[i], -1);
			ans += t.query(occF[i] - 1);
		}
 		
 		out.println(ans);

		out.flush();
		out.close();
	}

	static class BIT {
		int[] arr;

		BIT(int n) {
			arr = new int[n + 1];
		}

		int query (int idx) {
			int ans = 0;
			while(idx > 0) {
				ans += arr[idx];
				idx -= (idx & (-idx));
			}
			return ans;
		}

		void update(int idx, int v) {
			while(idx < arr.length) {
				arr[idx] += v;
				idx += (idx & (-idx));
			}
		}
	}



	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream System){br = new BufferedReader(new InputStreamReader(System));}
		public String next() throws IOException 
		{
			while (st == null || !st.hasMoreTokens()) 
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public String nextLine()throws IOException{return br.readLine();}
		public int nextInt() throws IOException {return Integer.parseInt(next());}
		public double nextDouble() throws IOException {return Double.parseDouble(next());}
		public char nextChar()throws IOException{return next().charAt(0);}
		public Long nextLong()throws IOException{return Long.parseLong(next());}
		public boolean ready() throws IOException{return br.ready();}
		public void waitForInput(){for(long i = 0; i < 3e9; i++);}
	}


}