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.StringTokenizer;
import java.util.HashMap;
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 Nasko
 */
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[] elem = new int[N];
            int[] left = new int[N];
            int[] right = new int[N];
            HashMap<Integer, Integer> a = new HashMap<Integer, Integer>();
            HashMap<Integer, Integer> b = new HashMap<Integer, Integer>();

            TaskD.Fenwick ft = new TaskD.Fenwick(N + 5);
            for (int i = 0; i < N; ++i) {
                elem[i] = in.nextInt();
                if (!a.containsKey(elem[i])) {
                    a.put(elem[i], 1);
                } else a.put(elem[i], a.get(elem[i]) + 1);
                left[i] = a.get(elem[i]);
            }
            for (int i = N - 1; i >= 0; --i) {
                if (!b.containsKey(elem[i])) {
                    b.put(elem[i], 1);
                } else {
                    b.put(elem[i], b.get(elem[i]) + 1);
                }
                right[i] = b.get(elem[i]);
            }
            long ans = 0L;
            for (int i = N - 1; i >= 0; --i) {
                ans += ft.get(left[i] - 1);
                ft.add(1, right[i]);
            }
            out.println(ans);
        }

        static class Fenwick {
            int N;
            int[] sum;

            public Fenwick(int _N) {
                N = _N;
                sum = new int[_N];
            }

            public void add(int value, int index) {


                while (index < sum.length - 5) {
                    sum[index] += value;
                    index += (index & (-index));
                }
            }

            public long get(int index) {

                long ret = 0L;
                while (index > 0) {
                    ret += sum[index];
                    index -= (index & (-index));
                }
                return ret;
            }

        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

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

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

    }
}

