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.util.*;
import java.io.*;

public class D
{
    static int[] Tree=new int[10000000];
    static int len=1000000+30;
    public static void main(String args[]) throws java.lang.Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        HashMap<Integer,Integer> m=new HashMap<Integer,Integer>();
        int N=Integer.parseInt(br.readLine());
        String[] S=br.readLine().split(" ");
        
        int[] A=new int[N];
        for(int i=0;i<N;i++)
        A[i]=Integer.parseInt(S[i]);

    
    int[] I=new int[N];
    int[] J=new int[N];
        
        int sumi=0,sumj=0,index=0;
    long count=0;
    for(int i=0;i<N;i++)
    {
        if(m.keySet().contains(A[i]))
                {
                    sumi=(Integer)m.get(A[i])+1;
                    m.put(A[i],sumi);
            I[i]=sumi;
                }
                else
                {
                    m.put(A[i],1);
            I[i]=1;
            }
    }
    m=new HashMap<Integer,Integer>();
    for(int rev=N-1;rev>=0;rev--)
    {
        if(m.keySet().contains(A[rev]))
                {
                    sumi=(Integer)m.get(A[rev])+1;
                   m.put(A[rev],sumi);
            J[rev]=sumi;
                }
                else
                {
                    m.put(A[rev],1);
            J[rev]=1;
            }
    }
    for(int i=0;i<N;i++)
    Update(J[i]+1,1);

    for(int i=0;i<N;i++)
    {
        Update(J[i]+1,-1);
        count+=Query(I[i]);
    }
    
        System.out.println(count);
    }

    static void Update(int k,int val)
    {
        while(k<len)
        {
            Tree[k]+=val;
            k+=k&(-k);
        }
    }
    
    static int Query(int node)
    {
        int sum=0;
        while(node>0)
        {
            sum+=Tree[node];
            node-=node&(-node);
        }
        return sum;
    }
}