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

public class A {
	
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		
		int n = sc.nextInt(), a[] = new int[n];
		for(int i = 0; i < n; ++i)
			a[i] = sc.nextInt();

		FenwickTree ft = new FenwickTree(n);
		
		HashMap<Integer, Integer> freq = new HashMap<>();
		int[] fsuffix = new int[n];
		for(int i = n - 1; i >= 0; --i)
		{
			Integer f = freq.get(a[i]);
			if(f == null)
				f = 0;
			fsuffix[i] = ++f;
			freq.put(a[i], f);
			ft.update(f, 1);
		}
		
		freq.clear();
		long ans = 0;
		for(int i = 0; i < n; ++i)
		{
			ft.update(fsuffix[i], -1);
			
			Integer f = freq.get(a[i]);
			if(f == null)
				f = 0;
			freq.put(a[i], ++f);
			ans += ft.query(f - 1);
		}
		
		out.println(ans);
		out.flush();
		out.close();
	}
	
	static class FenwickTree
	{
		int[] ft;
		
		FenwickTree(int n) { ft = new int[n + 1]; }
		
		void update(int idx, int val)
		{
			while(idx < ft.length)
			{
				ft[idx] += val;
				idx += idx & -idx;
			}
		}
		
		int query(int idx)
		{
			int s = 0;
			while(idx > 0)
			{
				s += ft[idx];
				idx ^= idx & -idx;
			}
			return s;
		}
	}
	
	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 double nextDouble() throws IOException { return Double.parseDouble(next()); }

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