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.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class ProblemD2 {
    BufferedReader rd;

    ProblemD2() throws IOException {
        rd = new BufferedReader(new InputStreamReader(System.in));
        compute();
    }

    private void compute() throws IOException {
        rd.readLine();
        int[] a = intarr();
        long res = solve(a);
        out(res);
    }

    private long solve(int[] a) {
        int n = a.length;
        Map<Integer, Integer> cm = new HashMap<>();
        int[] left = new int[n];
        for(int i=0;i<n;i++) {
            Integer c = cm.get(a[i]);
            if(c == null) {
                c = 1;
            } else {
                c++;
            }
            left[i] = c;
            cm.put(a[i], c);
        }
        cm = new HashMap<>();
        long res = 0;
        Fenwick f = new Fenwick();
        f.init(n+5);
        for(int i=n-1;i>=0;i--) {
            res += f.sum(left[i]-1);

            Integer c = cm.get(a[i]);
            if(c == null) {
                c = 1;
                f.add(1, 1);
            } else {
                c++;
                f.add(c, 1);
            }
            cm.put(a[i], c);
        }
        return res;
    }

    public class Fenwick {
        int[] s;

        public void init(int n) {
            s = new int[n];
        }

        public int sum(int r) {
            int ret = 0;
            for (; r >= 0; r = (r & (r+1)) - 1)
                ret += s[r];
            return ret;
        }

        public void add(int idx, int delta) {
            for (; idx < s.length; idx = idx | (idx+1))
                s[idx] += delta;
        }

    }

    private int[] intarr() throws IOException {
        return intarr(rd.readLine());
    }

    private int[] intarr(String s) {
        String[] q = split(s);
        int n = q.length;
        int[] a = new int[n];
        for(int i=0;i<n;i++) {
            a[i] = Integer.parseInt(q[i]);
        }
        return a;
    }

    public String[] split(String s) {
        if(s == null) {
            return new String[0];
        }
        int n = s.length();
        int start = -1;
        int end = 0;
        int sp = 0;
        boolean lastWhitespace = true;
        for(int i=0;i<n;i++) {
            char c = s.charAt(i);
            if(isWhitespace(c)) {
                lastWhitespace = true;
            } else {
                if(lastWhitespace) {
                    sp++;
                }
                if(start == -1) {
                    start = i;
                }
                end = i;
                lastWhitespace = false;
            }
        }
        if(start == -1) {
            return new String[0];
        }
        String[] res = new String[sp];
        int last = start;
        int x = 0;
        lastWhitespace = true;
        for(int i=start;i<=end;i++) {
            char c = s.charAt(i);
            boolean w = isWhitespace(c);
            if(w && !lastWhitespace) {
                res[x++] = s.substring(last,i);
            } else if(!w && lastWhitespace) {
                last = i;
            }
            lastWhitespace = w;
        }
        res[x] = s.substring(last,end+1);
        return res;
    }

    private boolean isWhitespace(char c) {
        return c==' ' || c=='\t';
    }

    private static void out(Object x) {
        System.out.println(x);
    }

    public static void main(String[] args) throws IOException {
        new ProblemD2();
    }
}
