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
/**
 * ******* Created  on 19/12/19 6:47 AM*******
 */

import java.io.*;
import java.util.*;

public class D459 implements Runnable {

    private static final int MAX = (int) (1E6 + 5);
    private static final int MOD = (int) (1E9 + 7);
    private static final long Inf = (long) (1E14 + 10);

    private void solve() throws IOException {
        int n = reader.nextInt();
        int[] a = new int[n];
        List<Integer> temp = new ArrayList<>();
        for(int i =0;i<n;i++){
            a[i] =reader.nextInt();
            temp.add(a[i]);
        }
        Collections.sort(temp);
        for(int i=0;i<n;i++)
            a[i] = Collections.binarySearch(temp, a[i]);
        int[] cnt =  new int[MAX];
        long ans = 0;
        int[] r = new int[n];
        for(int i =n-1;i>=0;i--){
            r[i]= ++cnt[a[i]];
        }
        Arrays.fill(cnt,0);

        for(int i =0;i<n;i++){
            ans += Get(r[i]+1);
            add(++cnt[a[i]]);
        }
        writer.print(ans);
    }

    private long Get(int val) {
        long res = 0;
        for(int i =val;i<MAX;i+= (i&(-i))){
            res += tree[i];
        }
        return res;
    }

    long[] tree = new long[MAX];
    private void add(int ind) {
        for(int i =ind;i>0;i-=(i&(-i))){
            tree[i]++;
        }
    }

    public static void main(String[] args) throws IOException {
        try (Input reader = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) {
            new D459().run();
        }
    }

    StandardInput reader;
    PrintWriter writer;

    @Override
    public void run() {
        try {
            reader = new StandardInput();
            writer = new PrintWriter(System.out);
            solve();
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    interface Input extends Closeable {
        String next() throws IOException;

        default int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        default long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        default double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        default int[] readIntArray() throws IOException {
            return readIntArray(nextInt());
        }

        default int[] readIntArray(int size) throws IOException {
            int[] array = new int[size];
            for (int i = 0; i < array.length; i++) {
                array[i] = nextInt();
            }
            return array;
        }

        default long[] readLongArray(int size) throws IOException {
            long[] array = new long[size];
            for (int i = 0; i < array.length; i++) {
                array[i] = nextLong();
            }
            return array;
        }
    }

    private static class StandardInput implements Input {
        private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        private StringTokenizer stringTokenizer;

        @Override
        public void close() throws IOException {
            reader.close();
        }

        @Override
        public String next() throws IOException {
            if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
                stringTokenizer = new StringTokenizer(reader.readLine());
            }
            return stringTokenizer.nextToken();
        }
    }
}
