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 Solution {
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        private 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 class Pair<U, V> {
        private final U first;       // first field of a Pair
        private final V second;      // second field of a Pair

        // Constructs a new Pair with specified values
        private Pair(U first, V second)
        {
            this.first = first;
            this.second = second;
        }

        @Override
        // Checks specified object is "equal to" current object or not
        public boolean equals(Object o)
        {
            if (this == o)
                return true;

            if (o == null || getClass() != o.getClass())
                return false;

            Pair<?, ?> pair = (Pair<?, ?>) o;

            // call equals() method of the underlying objects
            if (!first.equals(pair.first))
                return false;
            return second.equals(pair.second);
        }

        @Override
        // Computes hash code for an object to support hash tables
        public int hashCode()
        {
            // use hash codes of the underlying objects
            return 31 * first.hashCode() + second.hashCode();
        }

        @Override
        public String toString()
        {
            return "(" + first + ", " + second + ")";
        }

        // Factory method for creating a Typed Pair immutable instance
        public static <U, V> Pair <U, V> of(U a, V b)
        {
            // calls private constructor
            return new Pair<>(a, b);
        }
    }
    public static void update(int x, int val){
        for(;x<tree.length; x+=x&(-x)){
            tree[x]+=val;
        }
    }
    public static int query(int x){
        int res=0;
         for(;x>0; x-=x&(-x)){
             res+= tree[x];
         }
         return res;
    }
    static int[] tree;
    static int[] ar;
    static int[] fo;
    static int[] rev;
    public static void main(String[] args) throws IOException {
        FastReader in = new FastReader();
        PrintWriter out = new PrintWriter(System.out);
        int n=in.nextInt();
        ar= new int[n+1];
        fo= new int[n+1];
        rev= new int[n+1];
        int[] arr= new int[n+1];
        for(int i=1; i<=n;i++){
            arr[i]=in.nextInt();
        }
        Map<Integer,Integer> map =new HashMap<>();
        for(int i=1; i<=n;i++){
            map.put(arr[i], map.getOrDefault(arr[i],0)+1);
            fo[i]=map.get(arr[i]);
        }
        map.clear();
        for(int i=n; i>0;i--){
            map.put(arr[i], map.getOrDefault(arr[i],0)+1);
            rev[i]=map.get(arr[i]);
        }
        tree= new int[n+1];
        for(int i=1 ;i<=n;i++){
            update(rev[i],1);
        }
        long res=0;
        for(int i=1 ;i<=n; i++){
            update(rev[i],-1);
            res+= query(fo[i]-1);
        }
        out.println(res);
        out.flush();
        out.close();
    }
}