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 A {



    static class SegmentTree{
        private Pair[] tree;
        private int n, input[];
        public SegmentTree(int[] input){
            this.n = input.length;
            int[] temp = new int[n+1];
            for (int i = 1; i <= n; ++i){
                temp[i] = input[i-1];
            }
            this.input = temp;
            tree = new Pair[4*n+5];
            build(1, 1, n);
        }
        int l(int u){
            return 2*u;
        }
        int r(int u){
            return l(u)+1;
        }
        void build(int u, int left, int right){
            tree[u] = new Pair(left, right);
            if (left == right){
                tree[u].min = input[left];
                tree[u].max = input[left];
                tree[u].sum = input[left];
                return;
            }
            int mid = (left+right)/2;
            build(l(u), left, mid);
            build(r(u), mid+1, right);
            tree[u].min = Math.min(tree[l(u)].min, tree[r(u)].min);
            tree[u].max = Math.max(tree[l(u)].max, tree[r(u)].max);
            tree[u].sum = tree[l(u)].sum+tree[r(u)].sum;
        }
        void prop(int u){
            if (tree[u].left != tree[u].right){
                tree[l(u)].delta += tree[u].delta;
                tree[r(u)].delta += tree[u].delta;
                tree[u].min += tree[u].delta;
                tree[u].max += tree[u].delta;
                tree[u].delta = 0;
            }
        }
        void update(int u){
            if (tree[u].left != tree[u].right){
                tree[u].min = tree[l(u)].min+tree[l(u)].delta;
                tree[u].min = Math.min(tree[u].min,tree[r(u)].min+tree[r(u)].delta);
                tree[u].max = tree[l(u)].max+tree[l(u)].delta;
                tree[u].max = Math.max(tree[u].max,tree[r(u)].max+tree[r(u)].delta);
                tree[u].sum = tree[l(u)].sum+(tree[l(u)].right-tree[l(u)].left+1)*tree[l(u)].delta;
                tree[u].sum += tree[r(u)].sum+(tree[r(u)].right-tree[r(u)].left+1)*tree[r(u)].delta;
            }
        }
        int rangeMin(int left, int right){
           return rangeMin(1, left, right);
        }
        int rangeMax(int left, int right){
            return rangeMax(1, left, right);
        }
        long rangeSum(int left, int right){
            return rangeSum(1, left, right);
        }
        void rangeAdd(int left, int right, int val){
           rangeAdd(1, left, right, val);
        }
        int rangeMin(int u, int left, int right){
            if (tree[u].right < left || tree[u].left > right){
                return (int)1e9;
            }
            if (tree[u].left >= left && tree[u].right <= right){
                return tree[u].min+tree[u].delta;
            }
            prop(u);
            int min = rangeMin(l(u), left, right);
            min = Math.min(min, rangeMin(r(u), left, right));
            update(u);
            return min;
        }
        int rangeMax(int u, int left, int right){
            if (tree[u].right < left || tree[u].left > right){
                return (int)-1e9;
            }
            if (tree[u].left >= left && tree[u].right <= right){
                return tree[u].max+tree[u].delta;
            }
            prop(u);
            int max = rangeMax(l(u), left, right);
            max = Math.max(max, rangeMax(r(u), left, right));
            update(u);
            return max;
        }
        long rangeSum(int u, int left, int right){
            if (tree[u].right < left || tree[u].left > right){
                return 0L;
            }
            if (tree[u].left >= left && tree[u].right <= right){
                return tree[u].sum+(tree[u].right-tree[u].left+1)*tree[u].delta;
            }
            prop(u);
            long sum = rangeSum(l(u), left, right);
            sum += rangeSum(r(u), left, right);
            update(u);
            return sum;
        }
        void rangeAdd(int u, int left, int right, int add){
            if (tree[u].right < left || tree[u].left > right){
                return;
            }
            if (tree[u].left >= left && tree[u].right <= right){
                tree[u].delta += add;
                return;
            }
            prop(u);
            rangeAdd(l(u), left, right, add);
            rangeAdd(r(u), left, right, add);
            update(u);
        }
        private class Pair{
            int min, max, left, right;
            long sum;
            int delta = 0;
            public Pair(int l, int r){
                this.left = l;
                this.right = r;
                sum = 0L;
                min = (int)1e9;
                max = -min;
            }
        }
    }























    static boolean tests = false;

    static void solve(){
       int n = fs.nextInt();
       int[] a = fs.readIntArray(n);
       SegmentTree st = new SegmentTree(new int[n+1]);
       Map<Integer, Integer> cntRight = new HashMap<>();
       Map<Integer, Integer> cntLeft = new HashMap<>();
       int[] cnt = new int[n];
       for (int i = 0; i < n-1; ++i){
           cnt[i] = cntLeft.getOrDefault(a[i], 0)+1;
           cntLeft.put(a[i], cnt[i]);
       }
       long ans = 0;
       for (int i = n-1; i > 0; --i){
           int c = cntRight.getOrDefault(a[i], 0)+1;
           st.rangeAdd(c, c, 1);
           if (cnt[i-1] != 1) ans += st.rangeSum(1, cnt[i-1]-1);
           cntRight.put(a[i], c);
       }
       out.println(ans);
    }



























    static FastScanner fs;
    static PrintWriter out;
    static int int_max = (int)1e9+5, mod = int_max+2;
    public static void main(String[] args) {
        fs = new FastScanner();
        out = new PrintWriter(System.out);
        int t = 1;
        if (tests) t = fs.nextInt();
        while (t-- > 0) solve();
        out.close();
    }
    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readIntArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long[] readLongArray(int n){
            long a[] = new long[n];
            for (int i = 0; i < n; ++i){
                a[i] = nextLong();
            }
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble(){
            return Double.parseDouble(next());
        }
    }
}