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.lang.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    
    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        //Reader.init(System.in);
       // Scanner sc=new Scanner(System.in);
        FastReader sc=new FastReader(); 
        int n=sc.nextInt();
        ArrayList<node> list=new ArrayList<node>();
        for(int i=0;i<n;i++) {
            list.add(new node(sc.nextInt(),i));
        }
        Collections.sort(list,new sort_val());
        int val=1;
        int[] arr=new int[n];
        arr[list.get(0).index]=val;
        for(int i=1;i<n;i++) {
            node temp=list.get(i);
            if(temp.value==list.get(i-1).value) {
                arr[temp.index]=val;
            }
            else {
                arr[temp.index]=++val;
            }
        }
        int[]  count=new int[1000001];
        //int[] pref=new int[n];
        //int[] suff=new int[n];
        ArrayList<node> pref=new ArrayList<node>();
        ArrayList<node> suff=new ArrayList<node>();
        for(int i=0;i<n;i++) {
            count[arr[i]]++;
            pref.add(new node(count[arr[i]],i+1));
        }
        Arrays.fill(count,0);
        for(int i=n-1;i>=0;i--) {
            count[arr[i]]++;
            suff.add(new node(count[arr[i]],i+1));
        }
        Collections.sort(pref,new sort_val());
        Collections.sort(suff,new sort_val());
        int p1=0;
        int p2=0;
        bit tree=new bit(n);
        long ans=0;
//      for(int i=0;i<n;i++) {
//          System.out.print(pref.get(i).index+" "+pref.get(i).value+" || ");
//      }
//      System.out.println();
//      for(int i=0;i<n;i++) {
//          System.out.print(suff.get(i).index+" "+suff.get(i).value+" || ");
//      }
//      System.out.println();
        while(p1<n&& p2<n) {
        
            if(suff.get(p2).value<pref.get(p1).value) {
            tree.update(suff.get(p2).index, 1);
            p2++;
        }
        else {
            int temp=(tree.query(n)-tree.query(pref.get(p1).index));
            //System.out.println(temp);
            ans+=temp;
            p1++;
        }
        }
        if(p2==n) {
            while(p1<n) {
                ans+=(tree.query(n)-tree.query(pref.get(p1).index));
                p1++;
            }
        }
        System.out.println(ans);
    }
}  

class node{
    int value;
    int index;
    node(int value,int index){
        this.value=value;
        this.index=index;
        }
}
class node1{
    int type;
    int index;
}
class sort_val implements Comparator<node>{
    public int compare(node n1,node n2) {
        return n1.value-n2.value;
    }
}
class sort_val1 implements Comparator<node>{
    public int compare(node n1,node n2) {
        return n2.value-n1.value;
    }
}
class bit{
    int[] tree;
    int n;
    bit(int n){
        tree=new int[n+1];
        this.n=n;
    }
    public void update(int index,int value) {
        for(;index<=n;index+=index&(-index)) {
            tree[index]+=value;
        }
    }
    public int query(int index) {
        int  ans=0;
        for(;index>0;index-=index&(-index)) {
            ans+=tree[index];
        }
        return ans;
    }
}
class lazybit{
    bit tree1;
    bit tree2;
    int n;
    lazybit(int n){
        tree1=new bit(n);
        tree2=new bit(n);
        this.n=n;
    }
    public void update_range(int l,int r,int value){
        tree1.update(l, value);
        tree1.update(r+1, -1*(value));
        tree2.update(l,(l-1)*value);
        tree2.update(r+1, r*value*(-1));
    }
    public int query_range(int l,int r) {
        return ((tree1.query(r)*r-tree2.query(r))-(tree1.query(l-1)*(l-1)-tree2.query(l-1)));
    }
}
class edge{
    int vertex;
    int value;
    edge(int vertex,int value){
        this.vertex=vertex;
        this.value=value;
    }
}
class pair{
    int f;int s;
    pair(int f,int s){
        this.f=f;
        this.s=s;
    }
}
class a implements Comparator<edge>{
    public int compare(edge p1,edge p2) {
        return (int)(p1.vertex-p2.vertex);
    }
}
class FastReader 
{ 
    BufferedReader br; 
    StringTokenizer st; 

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

    String next() 
    { 
        while (st == null || !st.hasMoreElements()) 
        { 
            try
            { 
                st = new StringTokenizer(br.readLine()); 
            } 
            catch (IOException  e) 
            { 
                e.printStackTrace(); 
            } 
        } 
        return st.nextToken(); 
    } 

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

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

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

    String nextLine() 
    { 
        String str = ""; 
        try
        { 
            str = br.readLine(); 
        } 
        catch (IOException e) 
        { 
            e.printStackTrace(); 
        } 
        return str; 
    } 
} 

