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.ArrayList;
import java.util.Scanner;


public class mein 
{
    static int LE=1000666;
    static int[] A = new int[LE];
    static int[] B = new int[LE];
    static int[] Bi = new int[LE];
    static int MOD= 266613;
    static long result=0;
    static int n;
    static int[] Aib=new int[LE];
    static ArrayList<Integer>[] Hash=new ArrayList[MOD+666];
    static ArrayList<Integer>[] Hash_count=new ArrayList[MOD+666];
    
    public static void main(String[] Args)
    {
        Scanner f=new Scanner(System.in);
        n=f.nextInt();
        int i;
        
        for(i=1;i<=n;++i)  A[i]=f.nextInt();
        
        for(i=0;i<=MOD;++i) 
        {
            Hash[i]=new ArrayList<Integer>();
            Hash_count[i]=new ArrayList<Integer>();
        }
            
        solve();
        for(i=1;i<=n;++i) Bi[i]=B[i];
        
        for(i=0;i<=MOD;++i) 
        {
            Hash[i].clear();
            Hash_count[i].clear();
        }

        
        for(int st=1,dr=n;st<dr;++st,--dr) 
        {
              int aux=A[st];
              A[st]=A[dr];
              A[dr]=aux;
        }
        solve();
    
        
        
        for(int st=1,dr=n;st<dr;++st,--dr) {
              int aux=B[st];
              B[st]=B[dr];
              B[dr]=aux;
            }
        
        
        /*for(i=1;i<=n;++i) System.out.print(Bi[i]+" ");
        System.out.println();
        for(i=1;i<=n;++i) System.out.print(B[i]+" ");
        System.out.println();
        */
        
        
        for(i=n;i>0;--i)
        {
            result+=(long)aib_query(Bi[i]-1);
            //System.out.print(aib_query(Bi[i]-1)+" ");
            aib_add(B[i]);
        }
        
        System.out.println();
        
        System.out.println(result);
    
    }
    
    static void aib_add(int pos)
    {
        for(;pos<=n;pos+=lsb(pos)) Aib[pos]++;
    }
    
    static int aib_query(int pos)
    {
        int res=0;
        for(;pos>0;pos-=lsb(pos)) res+=Aib[pos];
        return res;
    }
    
    static int lsb(int val)
    {
        return (val&(val^(val-1)));
    }
    
    static void solve()
    {
        for(int i=1;i<=n;++i)
        {
            hash_update(A[i]);
            B[i]=hash_query(A[i]);  
        }
    }
    
    static void hash_update(int val)
    {
        int key=val%MOD;
        int N=Hash[key].size();
        int i;
        boolean okz=false;
        
        for(i=0;i<N;++i)
            if (Hash[key].get(i)==val)
                okz=true;
        
        if (okz==false) {Hash[key].add(val);Hash_count[key].add(0);++N;}
        
        for(i=0;i<N;++i)
            if (Hash[key].get(i)==val)
                Hash_count[key].set(i, Hash_count[key].get(i)+1);
    }
    
    static int hash_query(int val)
    {
        int key=val%MOD;
        int N=Hash[key].size(),i;
        
        for(i=0;i<N;++i)
            if (Hash[key].get(i)==val)
                return Hash_count[key].get(i);
        
        return 0;
    }
}