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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.TreeMap;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author xwchen
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = in.nextIntArray(n);
            int[] b = new int[n];
            for (int i = 0; i < n; ++i) {
                b[i] = a[i];
            }
            Arrays.sort(b);
            TreeMap<Integer, Integer> mp = new TreeMap<>();
            int m = 1;
            for (int i = 0; i < n; ++i) {
                if (!mp.containsKey(b[i])) {
                    mp.put(b[i], m++);
                }
            }
            for (int i = 0; i < n; ++i) {
                a[i] = mp.get(a[i]);
            }
            int[] fi = new int[n];
            int[] fj = new int[n];
            int[] cnt = new int[m];
            for (int i = 0; i < n; ++i) {
                fi[i] = ++cnt[a[i]];
            }
            Arrays.fill(cnt, 0);
            for (int i = n - 1; i >= 0; --i) {
                fj[i] = ++cnt[a[i]];
            }
            BinaryIndexedTree bit = new BinaryIndexedTree(n);
            long res = 0;
            for (int i = n - 1; i > 0; --i) {
                bit.insertElement(fj[i]);
                res += bit.findRank(fi[i - 1] - 1);
            }
            out.println(res);
        }

    }

    static class BinaryIndexedTree {
        int n;
        int[] bit;
        public int count;

        public BinaryIndexedTree(int n) {
            this.n = n;
            bit = new int[n + 1];
        }

        public void update(int i, int add) {
            while (i > 0 && i < n) {
                bit[i] += add;
                i = i + (i & (-i));
            }
        }

        public int sum(int i) {
            int ans = 0;
            while (i > 0) {
                ans += bit[i];
                i = i - (i & (-i));
            }

            return ans;
        }

        public void insertElement(int x) {
            update(x, 1);
            ++count;
        }

        public int findRank(int x) {
            return sum(x);
        }

    }

    static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer = new StringTokenizer("");

        public InputReader(InputStream inputStream) {
            this.reader = new BufferedReader(
                    new InputStreamReader(inputStream));
        }

        public String next() {
            while (!tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

    }
}

