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.*;
import java.util.*;
import java.util.stream.IntStream;

public class Solution {

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Solver solver = new Solver(in, out);
        solver.solve();
        out.close();
    }

    static class Solver {

        InputReader in;
        PrintWriter out;

        public Solver(InputReader in, PrintWriter out) {
            this.in = in;
            this.out = out;
        }

        int n;

        public void solve() {
            n = in.nextInt();
            int[] a = new int[n];
            int[] b = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
                b[i] = a[i];
            }
            b = IntStream.of(b).distinct().sorted().toArray();
            for (int i = 0; i < a.length; i++) {
                a[i] = getPosition(a[i], b) + 1;
            }

            int[] l = new int[a.length];
            int[] r = new int[a.length];
            int[] cnt = new int[b.length + 1];
            for (int i = 0; i < a.length; i++) {
                cnt[a[i]]++;
                l[i] = cnt[a[i]];
            }
            Arrays.fill(cnt, 0);
            for (int i = a.length - 1; i >= 0; i--) {
                cnt[a[i]]++;
                r[i] = cnt[a[i]];
            }

            SegTree tree = new SegTree(new int[a.length + 1]);
            long ans = 0;
            for (int i = a.length-1; i >= 0; i--) {
                ans += tree.getSum(0, l[i] - 1);
                tree.update(r[i], tree.getSum(r[i], r[i]) + 1);
            }

            out.print(ans);
        }

        private int getPosition(int x, int[] b) {
            int l = -1, r = b.length;
            while (r - l > 1) {
                int mid = (l + r) / 2;
                if (b[mid] < x) {
                    l = mid;
                } else {
                    r = mid;
                }
            }
            return r;
        }

        private int int2bool(int a) {
            if (a != 0) return 1;
            return 0;
        }

        private long binpow(long a, long n) {
            long res = 1;
            while (n > 0) {
                if (n % 2 == 1)
                    res *= a;
                a *= a;
                n >>= 1;
            }
            return res;
        }

        class IntIntPair implements Comparable<IntIntPair> {

            public Integer first;
            public Integer second;

            public IntIntPair() {
                this.first = -1;
                this.second = -1;
            }

            public IntIntPair(Integer first, Integer second) {
                this.first = first;
                this.second = second;
            }

            @Override
            public boolean equals(Object o) {
                if (this == o) return true;
                if (o == null || getClass() != o.getClass()) return false;
                IntIntPair that = (IntIntPair) o;
                return Objects.equals(first, that.first) &&
                        Objects.equals(second, that.second);
            }

            @Override
            public int hashCode() {

                return Objects.hash(first, second);
            }

            @Override
            public int compareTo(IntIntPair intIntPair) {
                if (first.equals(intIntPair.first)) {
                    return second.compareTo(intIntPair.second);
                }
                return first.compareTo(intIntPair.first);
            }
        }

        class Triple {
            int l;
            int r;
            int x;

            public Triple(int l, int r, int x) {
                this.l = l;
                this.r = r;
                this.x = x;
            }
        }
    }


    static class ArrayUtils {
        public static void fill(int[][] array, int value) {
            for (int[] row : array) {
                Arrays.fill(row, value);
            }
        }

    }

    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());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }

    static class SegTree {
        private int[] a;
        private int[] t;
        private int n;
        private boolean flag = true;

        public SegTree(int n) {
            a = new int[n];
            t = new int[n * 4];
        }

        public SegTree(int[] a) {
            this.a = a;
            n = a.length;
            t = new int[n * 4];
            build(1, 0, n - 1);
        }

        private void build(int v, int tl, int tr) {
            if (tl == tr) {
                t[v] = a[tl];
            } else {
                int tm = (tl + tr) / 2;
                build(v * 2, tl, tm);
                build(v * 2 + 1, tm + 1, tr);
            }
        }

        public int getSum(int l, int r) {
            return getSum(1, 0, n - 1, l, r);
        }

        private int getSum(int v, int tl, int tr, int l, int r) {
            if (l > r) {
                return 0;
            }
            if (tl == l && tr == r) {
                return t[v];
            }
            int tm = (tl + tr) / 2;
            return getSum(v * 2, tl, tm, l, Math.min(r, tm))
                    + getSum(v * 2 + 1, tm + 1, tr, Math.max(l, tm + 1), r);
        }

        public void update(int pos, int newVal) {
            update(1, 0, n - 1, pos, newVal);
        }

        private void update(int v, int tl, int tr, int pos, int newVal) {
            if (tl == tr)
                t[v] = newVal;
            else {
                int tm = (tl + tr) / 2;
                if (pos <= tm)
                    update(v*2, tl, tm, pos, newVal);
                else
                    update(v*2+1, tm+1, tr, pos, newVal);
                t[v] = t[v*2] + t[v*2+1];
            }
        }

        public void update(int l, int r, int color) {
            update(1, 0, n - 1, l, r, color);
        }

        private void push(int v) {
            if (t[v] != -1) {
                t[v * 2] = t[v * 2 + 1] = t[v];
                t[v] = -1;
            }
        }

        private void update(int v, int tl, int tr, int l, int r, int color) {
            if (l > r)
                return;
            if (l == tl && tr == r)
                t[v] = color;
            else {
                push(v);
                int tm = (tl + tr) / 2;
                update(v * 2, tl, tm, l, Math.min(r, tm), color);
                update(v * 2 + 1, tm + 1, tr, Math.max(l, tm + 1), r, color);
            }
        }

        public int get(int pos) {
            return get(1, 0, n - 1, pos);
        }

        private int get(int v, int tl, int tr, int pos) {
            if (tl == tr)
                return t[v];
            push(v);
            int tm = (tl + tr) / 2;
            if (pos <= tm) {
                return get(v * 2, tl, tm, pos);
            } else {
                return get(v * 2 + 1, tm + 1, tr, pos);
            }
        }
    }
}