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.SplittableRandom;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Pradyumn Agrawal coderbond007
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(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, FastReader in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = ArrayUtils.shrinkArray(in.nextIntArray(n));
            int[] num = new int[n];
            int[] fs = new int[n];
            int[] fe = new int[n];
            SegmentTree segmentTree = new SegmentTree(n) {

                public void merge(int c) {
                    seg[c] = seg[2 * c + 1] + seg[2 * c + 2];
                }


                public void joinDelta(int c, int val) {
                    seg[c] += val;
                }


                public void propagateLazy(int s, int e, int c) {

                }
            };
            for (int i = 0; i < n; i++) {
                fs[i] = ++num[a[i]];
            }
            ArrayUtils.fill(num, 0);
            for (int i = n - 1; i >= 0; i--) {
                fe[i] = ++num[a[i]];
            }
            long ret = 0;
            for (int i = n - 1; i >= 0; i--) {
                ret += segmentTree.getRange(0, fs[i] - 1);
                segmentTree.updateIndex(fe[i], 1);
            }
            out.println(ret);
        }

    }

    static class ArrayUtils {
        public static long[] sort(long[] a) {
            a = shuffle(a, new SplittableRandom());
            Arrays.sort(a);
            return a;
        }

        public static long[] shuffle(long[] a, SplittableRandom gen) {
            for (int i = 0, n = a.length; i < n; i++) {
                int ind = gen.nextInt(n - i) + i;
                long d = a[i];
                a[i] = a[ind];
                a[ind] = d;
            }
            return a;
        }

        public static void fill(int[] array, int value) {
            Arrays.fill(array, value);
        }

        public static int[] shrinkArray(int[] a) {
            int n = a.length;
            long[] b = new long[n];
            for (int i = 0; i < n; i++) {
                b[i] = (long) a[i] << 32 | i;
            }
            ArrayUtils.sort(b);
            int[] ret = new int[n];
            int p = 0;
            for (int i = 0; i < n; i++) {
                if (i > 0 && (b[i] ^ b[i - 1]) >> 32 != 0) {
                    p++;
                }
                ret[(int) b[i]] = p;
            }
            return ret;
        }

    }

    static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[8192];
        private int curChar;
        private int pnumChars;
        private FastReader.SpaceCharFilter filter;

        public FastReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (pnumChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= pnumChars) {
                curChar = 0;
                try {
                    pnumChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (pnumChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c == ',') {
                    c = read();
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

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

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static abstract class SegmentTree {
        public int[] a;
        public int[] seg;
        public int[] lazy;
        public int N;
        public int size;

        public SegmentTree(int[] array) {
            N = a.length;
            size = 4 * N;
            a = array.clone();
            seg = new int[size];
            lazy = new int[size];
            construct(0, N - 1, 0);
        }

        public SegmentTree(int N) {
            this.N = N;
            size = 4 * N;
            a = new int[N];
            seg = new int[size];
            lazy = new int[size];
        }

        public abstract void merge(int c);

        public abstract void joinDelta(int c, int val);

        public void construct(int s, int e, int c) {
            if (s == e) {
                joinDelta(c, a[s]);
                return;
            }
            int m = (s + e) >>> 1;
            construct(s, m, 2 * c + 1);
            construct(m + 1, e, 2 * c + 2);
            merge(c);
        }

        public void updateIndex(int ind, int val) {
            updateIndex(0, N - 1, 0, ind, val);
        }

        public void updateIndex(int s, int e, int c, int ind, int val) {
            if (s == e) {
                joinDelta(c, val);
                return;
            }
            int m = (s + e) >>> 1;
            if (ind <= m) {
                updateIndex(s, m, 2 * c + 1, ind, val);
            } else {
                updateIndex(m + 1, e, 2 * c + 2, ind, val);
            }
            merge(c);
        }

        public int getRange(int l, int r) {
            return getRange(0, N - 1, 0, l, r);
        }

        public int getRange(int s, int e, int c, int l, int r) {
            if (s > e || l > r || s > r || e < l) return 0;
            if (s == e) {
                return seg[c];
            }
            if (s >= l && e <= r) {
                return seg[c];
            }
            int m = (s + e) >>> 1;
            return getRange(s, m, 2 * c + 1, l, r) + getRange(m + 1, e, 2 * c + 2, l, r);
        }

    }
}