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.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * Created by chinh on 7/19/14.
 */
public class R261D2D {
    static class BinaryIndexedTree{
        private int[] bit;
        public BinaryIndexedTree(int size){
            bit = new int[size];
        }

        /**
         * @param index
         * @return sum from 0 -> index
         */
        public int query(int index){
            index++;
            int ret = 0;
            while(index>0){
                ret += bit[index-1];
                index -= index&-index;
            }
            return ret;
        }
        public void update(int index, int value){
            index++;
            int max = bit.length;
            while(index<=max){
                bit[index-1] += value;
                index += index&-index;
            }
        }
    }
    static class Mapper{
        private int[] v;
        public Mapper(int[] input){
            int[] a = Arrays.copyOf(input, input.length);
            Arrays.sort(a);
            int n=1;
            for(int i=0; i<a.length-1; i++){
                if(a[i]!=a[i+1]) n++;
            }
            v = new int[n];
            n = 0;
            for(int i=0; i<a.length-1; i++){
                if(a[i]!=a[i+1]) v[n++]=a[i];
            }
            v[n++]=a[a.length-1];
        }
        public int indexOf(int value){
            int l=0, r=v.length-1;
            while(l<=r){
                int mid = (l+r)/2;
                if(value>v[mid]) l=mid+1; else
                if(value<v[mid]) r=mid-1; else{
                    return mid;
                }
            }
            return -1;
        }
        public int getLength(){
            return v.length;
        }

    }
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] a = new int[n];
        StringTokenizer st = new StringTokenizer(br.readLine());
        for(int i=0; i<n; i++){
            a[i] = Integer.parseInt(st.nextToken());
        }
        Mapper mapper = new Mapper(a);
        int[] mapperCounter = new int[mapper.getLength()];
        int[] forwardCounter = new int[n];
        int[] backwardCounter = new int[n];
        for(int i=0; i<n; i++){
            int index = mapper.indexOf(a[i]);
            mapperCounter[index]++;
            forwardCounter[i]=mapperCounter[index];
        }
        Arrays.fill(mapperCounter, 0);
        for(int i=n-1; i>=0; i--){
            int index = mapper.indexOf(a[i]);
            mapperCounter[index]++;
            backwardCounter[i]=mapperCounter[index];
        }
        Mapper backwardMapper = new Mapper(backwardCounter);
        int m = backwardMapper.getLength();
        BinaryIndexedTree bit = new BinaryIndexedTree(m);
        mapperCounter = new int[m];
        for(int i=0; i<backwardCounter.length; i++){
            int index = backwardMapper.indexOf(backwardCounter[i]);
            mapperCounter[index]++;
        }
        for(int i=0; i<m; i++){
            bit.update(i, mapperCounter[i]);
        }
        long result=0;
        for(int i=0; i<n; i++){
            int index = backwardMapper.indexOf(backwardCounter[i]);
            bit.update(index, -1);
            index = backwardMapper.indexOf(forwardCounter[i]);
            if(index>0) result+=bit.query(index-1);
        }
        System.out.println(result);
    }
}
