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
// package CodeForces;

import java.awt.Point;
import java.io.*;
import java.util.*;

public class Problem_459D {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner();
		PrintWriter pw = new PrintWriter(System.out);
		int n=sc.nextInt();
		int[]a=new int[n];
		HashMap<Integer, Integer>occ=new HashMap<>();
		int fromstart[]=new int[n];
		for(int i=0;i<n;i++)
		{
			a[i]=sc.nextInt();
			if(occ.containsKey(a[i]))
			{
				int tmp=occ.get(a[i]);
				fromstart[i]=tmp+1;
				occ.put(a[i], tmp+1);
			}
			else
			{
				fromstart[i]=1;
				occ.put(a[i],1);
			}
		}
		occ=new HashMap<>();
		int fromlast[]=new int[n];
		for(int i=n-1;i>=0;i--)
		{
			if(occ.containsKey(a[i]))
			{
				int tmp=occ.get(a[i]);
				fromlast[i]=tmp+1;
				occ.put(a[i], tmp+1);
			}
			else
			{
				fromlast[i]=1;
				occ.put(a[i],1);
			}
		}
//		System.out.println(Arrays.toString(fromstart));
//		System.out.println(Arrays.toString(fromlast));
		long ans=0;
		FenwickTree ft=new FenwickTree(a.length+1);
//		System.out.println();
		for(int i=n-1;i>=0;i--)
		{
//			System.out.println(Arrays.toString(ft.ft));
			ans+=(long)ft.prefix(fromstart[i]);
			ft.increment(fromlast[i], 1);
		}
		System.out.println(ans);
		pw.close();
	}
	
	static class FenwickTree
	{
		int[]a;
		int[]ft;
		public FenwickTree(int n)
		{
			ft=new int[n];
//			build();
		}
		public void build()
		{
			for(int i=0;i<a.length;i++)
				increment(i,a[i]);//since all values are zeros
		}
		public void increment(int i,int v)
		{
			i++;//since ft is index 1 based;
			while(i<ft.length)
			{
				ft[i]+=v;
				i+=(i&(-i));
			}
		}
		public void update(int i,int v)
		{
			int diff=v-a[i];
			a[i]=v;
			increment(i,diff);
		}
		public int query(int l,int r)
		{
			l++;r++;
			return prefix(r)-prefix(l-1);
		}
		public int prefix(int n)
		{
			int x=0;
			while(n>0)
			{
				x+=ft[n];
				n-=(n&(-n));
			}
			return x;
		}
		public String toString()
		{
			String s="";
			for(int i=1;i<ft.length;i++)
				s+=ft[i]+" ";
			return s;
		}
	}
	
	static class MyComp implements Comparator<Point>{
		 
	    @Override
	    public int compare(Point p1, Point p2) {
	        if(p1.x==p2.x)
	        	return p1.y-p2.y;
	        return p1.x-p2.x;
	    }
	     
	}
	
	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

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

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

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

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

		double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

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

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

	}
}
