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.InputStreamReader;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Random;


public class Main {


	public static class pair implements Comparable<pair>
	{
		int a;
		int b;
		public pair(int pa, int pb)
		{
			a = pa; b= pb;
		}
		@Override
		public int compareTo(pair o) {
			if(this.a < o.a)
				return -1;
			if(this.a > o.a)
				return 1;
			return Integer.compare(o.b, this.b);
		}
	}


	//int n = Integer.parseInt(in.readLine());
	//int n = Integer.parseInt(spl[0]);
	//String[] spl = in.readLine().split(" ");

	public static int[] tree;
	public static int MaxVal = 1000005;

	public static int read(int idx){
		int sum = 0;
		while (idx > 0){
			sum += tree[idx];
			idx -= (idx & -idx);
		}
		return sum;
	}
	public static void update(int idx ,int val){
		while (idx <= MaxVal){
			tree[idx] += val;
			idx += (idx & -idx);
		}
	}
	
	public static void main (String[] args) throws Exception
	{	
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(in.readLine());
		String[] spl = in.readLine().split(" ");
		int[] a = new int[n];
		int[] idx = new int[n];
		HashMap<Integer,Integer> tot = new HashMap<Integer,Integer>();
		for(int i=0; i<n;i++)
		{
			a[i] = Integer.parseInt(spl[i]);
			if(tot.containsKey(a[i]))
				tot.put(a[i], tot.get(a[i])+1);
			else
				tot.put(a[i], 1);
			idx[i] = tot.get(a[i]);
		}
		tree = new int[1000020];
		long ans = 0;
		for(int i=0; i<n; i++)
		{
			int idxRev = tot.get(a[i])-idx[i]+1;
			ans += read(MaxVal)-read(idxRev);
			update(idx[i],1);
		}
		System.out.println(ans);
	}
}