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.InputStreamReader;
import java.io.IOException;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Anirudh "code_overlord" Rayabharam
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskD {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
        }

        int[] dp = new int[n];
        int x;
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < n; i++) {
            if (map.containsKey(a[i])) {
                x = map.get(a[i]) + 1;
                map.put(a[i], x);
            }
            else {
                map.put(a[i], 1);
                x = 1;
            }
            dp[i] = x;
        }

        NumbersFenwickTree fTree = new NumbersFenwickTree(n);
        map.clear();
        long ans = 0;
        for (int i = n-1; i >= 0; --i) {
            ans += fTree.query(dp[i]-1);
            x = 1;
            if (map.containsKey(a[i])) {
                x = map.get(a[i]) + 1;
                map.put(a[i], x);
            }
            else map.put(a[i], 1);
            fTree.update(x, 1);
        }

        out.println(ans);
    }
}

class InputReader {
    public BufferedReader reader;
    private int tokenCount, nextTokenIndex;
    private String[] tokens;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenCount = nextTokenIndex = 0;
    }

    public String next() {
        String nextLine;
        if (nextTokenIndex == tokenCount) {
            try {
                nextLine = reader.readLine();
                nextTokenIndex = 0;
                tokens = nextLine.split("\\s");
                tokenCount = tokens.length;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return tokens[nextTokenIndex++];
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

}

class NumbersFenwickTree {
    protected long[] tree;
    protected int size;

    public NumbersFenwickTree(int size) {
        this.size = size;
        tree = new long[size+1];
    }

    public long query(int index) {
        long sum = 0;
        while (index > 0) {
            sum += tree[index];
            index -= index & (-index);
        }

        return sum;
    }

    public void update(int index, long value) {
        while (index <= size) {
            tree[index] += value;
            index += index & (-index);
        }
    }
}

