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

/**
 * 459D
 *
 * The algorithm uses the divide-and-conquer approach.
 *
 * O(n*log(n)) time
 * O(n*log(n)) memory
 *
 * @author artyom
 */
public class PashmakAndParmidasProblem implements Runnable {
    private BufferedReader in;
    private PrintStream out;
    private StringTokenizer tok;

    private void solve() throws IOException {
        int n = nextInt();
        int[] a = readIntArray(n);
        int[] pref = evalPrefixes(a);
        int[] suf = evalSuffixes(a);
        out.print(mergeSortAndCount(pref, suf, 0, n - 1));
    }

    private static int[] evalPrefixes(int[] a) {
        Map<Integer, Integer> w = new HashMap<>();
        int[] f = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            if (!w.containsKey(a[i])) {
                w.put(a[i], 0);
            }
            w.put(a[i], w.get(a[i]) + 1);
            f[i] = w.get(a[i]);
        }
        return f;
    }

    private static int[] evalSuffixes(int[] a) {
        Map<Integer, Integer> w = new HashMap<>();
        int[] f = new int[a.length];
        for (int i = a.length - 1; i >= 0; i--) {
            if (!w.containsKey(a[i])) {
                w.put(a[i], 0);
            }
            w.put(a[i], w.get(a[i]) + 1);
            f[i] = w.get(a[i]);
        }
        return f;
    }

    private static long mergeSortAndCount(int[] pref, int[] suf, int p, int q) {
        if (p < q) {
            int r = (p + q) / 2;
            return mergeSortAndCount(pref, suf, p, r) + mergeSortAndCount(pref, suf, r + 1, q) + mergeAndCount(pref, suf, p, r, q);
        }
        return 0;
    }

    private static long mergeAndCount(int[] pref, int[] suf, int l, int m, int r) {
        int n1 = m - l + 1;
        int n2 = r - m;
        int[] lPref = new int[n1 + 1];
        int[] rPref = new int[n2 + 1];
        int[] lSuf = new int[n1 + 1];
        int[] rSuf = new int[n2 + 1];
        for (int i = 0; i < n1; i++) {
            lPref[i] = pref[l + i];
            lSuf[i] = suf[l + i];
        }
        for (int i = 0; i < n2; i++) {
            rPref[i] = pref[m + i + 1];
            rSuf[i] = suf[m + i + 1];
        }
        lPref[n1] = rPref[n2] = lSuf[n1] = rSuf[n2] = Integer.MAX_VALUE;
        long counter = 0;
        for (int k = l, i = 0, j = 0; k <= r; k++) {
            if (lPref[i] <= rSuf[j]) {
                i++;
            } else {
                j++;
                counter += n1 - i;
            }
        }
        for (int k = l, i = 0, j = 0; k <= r; k++) {
            if (lPref[i] <= rPref[j]) {
                pref[k] = lPref[i];
                i++;
            } else {
                pref[k] = rPref[j];
                j++;
            }
        }
        for (int k = l, i = 0, j = 0; k <= r; k++) {
            if (lSuf[i] <= rSuf[j]) {
                suf[k] = lSuf[i];
                i++;
            } else {
                suf[k] = rSuf[j];
                j++;
            }
        }
        return counter;
    }

    //--------------------------------------------------------------
    public static void main(String[] args) {
        new PashmakAndParmidasProblem().run();
    }

    @Override
    public void run() {
        try {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = System.out;
            tok = null;
            solve();
            in.close();
        } catch (IOException e) {
            System.exit(0);
        }
    }

    private String nextToken() throws IOException {
        while (tok == null || !tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    private int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    private int[] readIntArray(int n) throws IOException {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextInt();
        }
        return arr;
    }
}