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.*;

public class a {
    static class FastInput
    {
        final private int BUFFER_SIZE = 1 << 16;

        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public FastInput(InputStream in)
        {
            din = new DataInputStream(in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public long nextLong() throws Exception
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = c == '-';
            if (neg) c = read();
            do
            {
             ret = ret * 10 + c - '0';
             c = read();
            } while (c > ' ');
            if (neg) return -ret;
            return ret;
        }

        //reads in the next string
        public String next() throws Exception
        {
            StringBuilder ret =  new StringBuilder();
            byte c = read();
            while (c <= ' ') c = read();
            do
            {
                ret = ret.append((char)c);
                c = read();
            } while (c > ' ');
            return ret.toString();
        }

        public int nextInt() throws Exception
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = c == '-';
            if (neg) c = read();
            do
            {
                ret = ret * 10 + c - '0';
                c = read();
            } while (c > ' ');
            if (neg) return -ret;
            return ret;
        }

        private void fillBuffer() throws Exception
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1) buffer[0] = -1;
        }

        private byte read() throws Exception
        {
            if (bufferPointer == bytesRead) fillBuffer();
            return buffer[bufferPointer++];
        }
    }
    public static void merge(int[] f, int l, int mid, int r) {
        int[] res = new int[r - l + 1];
        int i = l, j = mid + 1, k = 0;
        while (i <= mid && j <= r) {
            if (f[i] < f[j]) {
                res[k++] = f[i++];
            }
            else {
                res[k++] = f[j++];
            }
        }
        while (i <= mid) {
            res[k++] = f[i++];
        }
        while (j <= r) {
            res[k++] = f[j++];
        }
        for (i = l; i <= r; ++i) {
            f[i] = res[i - l];
        }
    }
    public static long solve(int[] fl, int[] fr, int l, int r) {
        if (l == r) {
            return 0;
        }

        int mid = (l + r) / 2;
        long res = solve(fl, fr, l, mid) + solve(fl, fr, mid + 1, r);

        for (int i = l, j = mid + 1; i <= mid; ++i) {
            while (j <= r && fr[j] < fl[i]) {
                ++j;
            }
            res += j - mid - 1;
        }

        merge(fl, l, mid, r);
        merge(fr, l, mid, r);
        return res;
    }
	public static void main(String args[]) throws Exception {
        FastInput sc = new FastInput(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] fl = new int[n];
        int[] fr = new int[n];
        Map<Integer, Integer> cnt = new HashMap<>();

        for (int i = 0; i < n; ++i) {
            a[i] = sc.nextInt();
            if (cnt.containsKey(a[i])) {
                cnt.put(a[i], cnt.get(a[i]) + 1);
            }
            else {
                cnt.put(a[i], 1);
            }
            fl[i] = cnt.get(a[i]);
        }
        
        cnt.clear();
        for (int i = n - 1; i >= 0; --i) {
            if (cnt.containsKey(a[i])) {
                cnt.put(a[i], cnt.get(a[i]) + 1);
            }
            else {
                cnt.put(a[i], 1);
            }
            fr[i] = cnt.get(a[i]);
        }

        System.out.print(solve(fl, fr, 0, n - 1));
    }
}
