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 Solution
{
     static  long getSum(int index , long BITree[]) 
    { 
        long sum = 0; 
        while(index>0) 
        { 
            
            sum += BITree[index]; 
      
          
            index -= index & (-index); 
        } 
        return sum; 
    } 
  
   
    public static void updateBIT(int n, int index,  
                                        int val , long BITree[]) 
    { 
        while(index <= n) 
        { 
      
        BITree[index] += val; 
        index += index & (-index); 
        } 
    } 
       public static void main(String []args) throws IOException
       {
                BufferedReader br = new BufferedReader( 
                              new InputStreamReader(System.in)); 
  
        StringTokenizer st = new StringTokenizer(br.readLine()); 
              
             int n = Integer.parseInt(st.nextToken());
             st = new StringTokenizer(br.readLine());
             
             int arr[] = new int[n];
             long tot[] = new long[n+1];
             HashMap<Integer,Integer> end = new HashMap<Integer,Integer>();
              HashMap<Integer,Integer> begin = new HashMap<Integer,Integer>();
              
              long cnt[] = new long[n+1];
             for(int i = 0 ; i < n ; i++)
             {
                    arr[i] = Integer.parseInt(st.nextToken());
                    if(end.containsKey(arr[i]))
                    end.replace(arr[i] , end.get(arr[i])+1);
                    
                    else
                    end.put(arr[i],1);
                    
                    updateBIT(n,end.get(arr[i]) , end.get(arr[i]) , cnt);
                    updateBIT(n,end.get(arr[i]) , 1 , tot);
                    if(end.get(arr[i]) != 1)
                    {
                           updateBIT(n,end.get(arr[i])-1 , -(end.get(arr[i])-1),cnt);
                           updateBIT(n,end.get(arr[i])-1 , -1 , tot);
                    }
             }
             long ans = 0;
            
             for(int i = 0 ; i < n ; i++)
             {
                    if(begin.containsKey(arr[i]))
                    begin.replace(arr[i] , begin.get(arr[i])+1);
                    
                    else
                    begin.put(arr[i],1);
                    
                
                    end.replace(arr[i] , end.get(arr[i])-1);
                
                if(end.get(arr[i]) != 0)
                {
                updateBIT(n,end.get(arr[i]) , end.get(arr[i]) , cnt);
                 updateBIT(n,end.get(arr[i]) , 1 , tot);
                }
                
                    
                           updateBIT(n,end.get(arr[i])+1 , -(end.get(arr[i])+1),cnt);
                           updateBIT(n,end.get(arr[i])+1 , -1,tot);
                       
                       ans += getSum(begin.get(arr[i])-1,cnt);
                       long x = getSum(n,tot)-getSum(begin.get(arr[i])-1,tot);
                       long y = begin.get(arr[i])-1;
                      
                       ans += x*y;
                
             }
             System.out.println(ans);
       }
}