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.*;
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Main
{
    static StringTokenizer st=new StringTokenizer("");
    static BufferedReader br;
    
    ///////////////////////////////////////////////////////////////////////////////////
    
   
    public static void main(String args[]) throws FileNotFoundException, IOException, Exception
    {
        //Scanner in =new Scanner(new FileReader("input.txt"));
        //PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
        //Scanner in=new Scanner(System.in);
        br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out=new PrintWriter(System.out);
        //StringTokenizer st;//=new StringTokenizer(br.readLine());
        //////////////////////////////////////////////////////////////////////////////////////
        
        int n=ni();
        int arr[]=new int[n];
        for(int i=0;i<n;i++)arr[i]=ni();
        int f[]=new int[n];
        int s[]=new int[n];
        TreeMap<Integer,Integer> map=new TreeMap<Integer,Integer>();
        for(int i=0;i<n;i++)
        {
            if(map.containsKey(arr[i]))
            {
                s[i]=s[map.get(arr[i])]+1;
                map.put(arr[i],i);
            }
            else
            {
                s[i]=1;
                map.put(arr[i],i);
            }
        }
        map.clear();
        for(int i=n-1;i>=0;i--)
        {
            if(map.containsKey(arr[i]))
            {
                f[i]=f[map.get(arr[i])]+1;
                map.put(arr[i],i);
            }
            else
            {
                f[i]=1;
                map.put(arr[i],i);
            }
        }
        BIT tree=new BIT(n);
        long res=0;
        for(int i=n-1;i>=0;i--)
        {
            res+=tree.sum(s[i]-1);
            tree.insert(f[i],1);
        }
        out.print(res);
        //////////////////////////////////////////////////////////////////////////////////////
	out.close();
    }
    static class BIT
    {
        private int c[];
        BIT(int n){c=new int[n+1];}
        public int lowbit(int i)
        {
            return i&(i^(i-1));
        }
        public int sum(int k)
        {
            int res=0;
            while(k>0)
            {
                res+=c[k];
                k-=lowbit(k);
            }
            return res;
        }
        public void insert(int i,int v)
        {
            while(i<c.length)
            {
                c[i]+=v;
                i+=lowbit(i);
            }
        }
    }
    ///////////////////////////////////////////////////////////////////////////////
    
    private static long gcd(long a,long b)
    {
        if(b==0)
            return a;
        return gcd(b,a%b);
    }
    private static class pair implements Comparable<pair>
    {
        int first;
        int second;
        pair()
        {
            first=0;
            second=0;
        }
        pair(int f,int s)
        {
            first=f;
            second=s;
        }

        public int compareTo(pair o)
        {
            if(first+second >  o.first+o.second)
                return 1;
            else if(first+second < o.first+o.second)
                return -1;
            else
                return 0;
        }
    }
    
    public static Integer ni() throws Exception
    {
        if(!st.hasMoreTokens())
            st=new StringTokenizer(br.readLine());
        return Integer.parseInt(st.nextToken());
    }
    public static BigInteger nb()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return BigInteger.valueOf(Long.parseLong(st.nextToken()));
    }
    public static Long nl() throws Exception
    {
        if(!st.hasMoreTokens())
            st=new StringTokenizer(br.readLine());
        return Long.parseLong(st.nextToken());
    }
    public static Double nd()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return Double.parseDouble(st.nextToken());
    }
    public static String ns()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return st.nextToken();
    }
}