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
// package SegmentTree;

import javafx.util.Pair;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class PashmakAndParmida {
    public static int query(int tree[],int s,int e,int q,int node){
        if(e<=q){
            return tree[node];
        }
        if(s>q){
            return 0;
        }
        int mid=(s+e)/2;
        int left=query(tree,s,mid,q,2*node);
        int right=query(tree,mid+1,e,q,2*node+1);
        return left+right;
    }
   public static void update(int tree[],int s,int e,int ind,int node){
       if(s==e){
           tree[node]++;
           return;
       }
       int mid=(s+e)/2;
       if(ind<=mid){
           update(tree,s,mid,ind,2*node);
       }
       else{
           update(tree,mid+1,e,ind,2*node+1);
       }
       tree[node]=tree[2*node]+tree[2*node+1];
   }
    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        int n=Integer.parseInt(br.readLine());
        HashMap<Integer,Integer> map=new HashMap<>();
        st=new StringTokenizer(br.readLine());
        TreeSet<Integer> set=new TreeSet<>();
        int a[]=new int[n+1];
        for(int i=1;i<=n;i++){
            a[i]=Integer.parseInt(st.nextToken());
            set.add(a[i]);
        }
        int count=0;
        int tree[]=new int[4*n+4];
        for(int k:set){
            map.put(k,count++);
        }
        int freq[]=new int[count];
        for(int i=1;i<=n;i++){
            a[i]=map.get(a[i]);
            freq[a[i]]++;
        }
        long ans=0;
        int freqb[]=new int[n+1];
        for(int i=n;i>=1;i--){
            int t=query(tree,1,n,freq[a[i]]-1,1);
//            System.out.println(t);
            ans+=t;
            freqb[a[i]]++;
            freq[a[i]]--;
            update(tree,1,n,freqb[a[i]],1);
        }
        System.out.println(ans);
    }
}
