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

/**
 * Created by hama_du on 2014/08/24.
 */
public class ProblemD {
    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n ; i++) {
            a[i] = in.nextInt();
        }
        int[] c = compress(a);
        int[] deg = new int[n];
        int[] lval = new int[n];
        for (int i = 0 ; i < n ; i++) {
            deg[c[i]]++;
            lval[i] = deg[c[i]];
        }
        int[] rval = new int[n];
        Arrays.fill(deg, 0);
        for (int i = n-1 ; i >= 0 ; i--) {
            deg[c[i]]++;
            rval[i] = deg[c[i]];
        }

        BIT right = new BIT(n+10);
        for (int i = 0 ; i < n ; i++) {
            right.add(rval[i], 1L);
        }

        long pairs = 0;
        for (int i = 0 ; i < n ; i++) {
            right.add(rval[i], -1L);
            int to = lval[i]-1;
            if (to >= 1) {
                pairs += right.range(1, to);
            }
        }
        System.out.println(pairs);
    }


    static class BIT {
        long N;
        long[] data;
        BIT(int n) {
            N = n;
            data = new long[n+1];
        }

        long sum(int i) {
            long s = 0;
            while (i > 0) {
                s += data[i];
                i -= i & (-i);
            }
            return s;
        }

        long range(int i, int j) {
            return sum(j) - sum(i-1);
        }

        void add(int i, long x) {
            while (i <= N) {
                data[i] += x;
                i += i & (-i);
            }
        }
    }



    static void d() {
        System.out.println(1000000);
        for (int i = 1 ; i <= 1000000 ; i++) {
            if (i > 1) {
                System.out.print(' ');
            }
            System.out.print(i);
        }
        System.out.println();
    }

    static int[] compress(int[] a) {
        int n = a.length;
        Set<Integer> set = new HashSet<>();
        for (int i : a) {
            set.add(i);
        }
        List<Integer> list = new ArrayList<>();
        for (int s : set) {
            list.add(s);
        }
        Collections.sort(list);

        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0 ; i < list.size() ; i++) {
            map.put(list.get(i), i);
        }
        int[] ret = new int[n];
        for (int i = 0 ; i < n ; i++) {
            ret[i] = map.get(a[i]);
        }
        return ret;
    }



    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

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

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

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

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

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

    public static void debug(Object... o) {
        System.err.println(Arrays.deepToString(o));
    }
}
