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.*;


public class p68 {
    static 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; 
        } 
    }
    static int n;
    static int[] t;

    static void modify(int p, int val) {
        for( t[p+=n]+=val;p>1;p>>=1)
            t[p>>1] += val;
    }

    static long query(int l, int r){ //[l, r)
        long res = 0;
        for(l+=n, r+=n; l<r; l>>=1, r>>=1){
            if((l&1)==1) res+=t[l++];
            if((r&1)==1) res+=t[--r];
        }
        return res;
    }

    public static void main(String[] args){
        FastReader sc = new FastReader();
        n = sc.nextInt();
        long[] arr = new long[n];
        int[] l = new int[n];
        int[] r = new int[n];
        t = new int[2*n];

        for(int i=0;i<n;i++)
            arr[i] = sc.nextLong();

        Map<Long, Integer> map = new HashMap<>();
        for(int i=0;i<n;i++){
            map.put(arr[i], map.getOrDefault(arr[i], 0)+1);
            l[i] = map.get(arr[i]);
        }

        map = new HashMap<>();
        for(int i=n-1;i>=0;i--){
            map.put(arr[i], map.getOrDefault(arr[i], 0)+1);
            r[i] = map.get(arr[i]);
        }

        long ans = 0;
        for(int i=n-1;i>=0;i--){
            // System.out.println(query(1, l[i]));
            ans += query(0, l[i]);
            if(i>0)
                modify(r[i], 1);
        }

        System.out.println(ans);
    }
}