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

public class PashmakParmidasProblem {
    public static void main(String[] args) {
        FastScanner scanner = new FastScanner();
        PrintWriter out = new PrintWriter(System.out, false);
        int n = scanner.nextInt();
        int[] arr = new int[n];
//        TreeSet<Integer> set = new TreeSet<>();
        for(int i = 0; i < n;i++) {
            arr[i] = scanner.nextInt();
//            set.add(arr[i]);
        }
        int[] forward = new int[n];
        int[] rev = new int[n];
        int max = 0;
        HashMap<Integer,Integer> prev = new HashMap<>();
        for(int i = 0; i <n; i++) {
            Integer other = prev.get(arr[i]);
            if (other != null) {
                forward[i] = forward[other] + 1;
            }
            else {
                forward[i] = 1;
            }
            max = Math.max(forward[i], max);
            prev.put(arr[i], i);
        }
        prev.clear();
        for(int i = n-1; i >= 0; i--) {
            Integer other = prev.get(arr[i]);
            if (other != null) {
                rev[i] = rev[other] + 1;
            }
            else {
                rev[i] = 1;
            }
            max = Math.max(rev[i], max);
            prev.put(arr[i], i);
        }
        BIT bit = new BIT(max + 5);
        bit.update(forward[0], 1);
        long ans = 0;
        for(int i = 1; i < n; i++) {
            ans += bit.range(rev[i] + 1, max + 4);
            bit.update(forward[i],1);
        }
        out.println(ans);
        out.flush();
    }
    static class BIT {
        long[] table;
        int sz;
        public BIT(int ss) {
            sz = ss;
            table = new long[ss];
        }
        void update(int loc, long amt) {
            while(loc < sz) {
                table[loc] += amt;
                loc += loc & - loc;
            }
        }
        long pref(int loc) {
            long ret = 0;
            while(loc > 0) {
                ret += table[loc];
                loc -= loc & - loc;
            }
            return ret;
        }
        long range(int l, int r) {
            return pref(r) - pref(l - 1);
        }
    }
    public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        
        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }
        
        public FastScanner() {
            this(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 readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}
