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.HashMap;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Rene
 */
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) {
            HashMap<Integer, Integer> map = new HashMap<>();
            int n = in.nextInt();
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                int z = in.nextInt();
                if (!map.containsKey(z)) map.put(z, map.size());
                a[i] = map.get(z);
            }
            int[] count = new int[map.size()];
            for (int i = 0; i < n; i++) count[a[i]]++;
            int[] total = Arrays.copyOf(count, count.length);
            SegmentTree countTree = new SegmentTree(n + 1);
            long answer = 0;
            for (int i = n - 1; i >= 0; i--) {
                answer += countTree.query(1, count[a[i]]);
                count[a[i]]--;
                countTree.changeDelta(total[a[i]] - count[a[i]], 1);
            }
            out.println(answer);
        }

        public class SegmentTree {
            private int[] x;
            private int n;

            public SegmentTree(int cap) {
                n = cap;
                x = new int[2 * n];
            }

            public SegmentTree(int[] a) {
                n = a.length;
                x = new int[2 * n];
                System.arraycopy(a, 0, x, n, n);
                for (int i = n - 1; i > 0; i--) x[i] = x[2 * i] + x[2 * i + 1];
            }

            public void change(int k, int v) {
                k += n;
                x[k] = v;
                for (; k > 1; k >>= 1) x[k >> 1] = x[k] + x[k ^ 1];
            }

            public void changeDelta(int k, int delta) {
                change(k, x[k + n] + delta);
            }

            public int query(int l, int r) {
                int result = 0;
                for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
                    if (l % 2 == 1) result += x[l++];
                    if (r % 2 == 1) result += x[--r];
                }
                return result;
            }

        }

    }

    static class InputReader {
        public InputStream stream;
        private int current;
        private int size;
        private byte[] buffer = new byte[10000];

        public InputReader(InputStream stream) {
            this.stream = stream;
            current = 0;
            size = 0;
        }

        int read() {
            int result;
            try {
                if (current >= size) {
                    current = 0;
                    size = stream.read(buffer);
                    if (size < 0) return -1;
                }
            } catch (IOException e) {
                throw new RuntimeException();
            }
            return buffer[current++];
        }

        public int nextInt() {
            int sign = 1;
            int result = 0;
            int c = readNextValid();
            if (c == '-') {
                sign = -1;
                c = read();
            }
            do {
                if (c < '0' || c > '9') throw new RuntimeException();
                result = 10 * result + c - '0';
                c = read();
            } while (!isEmpty(c));
            result *= sign;
            return result;
        }

        private int readNextValid() {
            int result;
            do {
                result = read();
            } while (isEmpty(result));
            return result;
        }

        private boolean isEmpty(int c) {
            return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == -1;
        }

    }
}

