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

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

class TaskD {
	OutputWriter out;
	InputReader in;
	static final int LAST = 0;
	static final int FIRST = 1;
	public void solve(int testNumber, InputReader in, OutputWriter out) {
		this.in = in; this.out = out;
		final int N = in.readInt();
		int[] a = new int[N];
		for (int i = 0; i < N; i++) {
			a[i] = in.readInt();
		}
		int[][] count = getCount(N, a);
		int[] bit = new int[N + 10];
		long res = 0;
		for (int i = N - 1; i >= 0; i--) {
			res += sum(bit, count[i][LAST]);
			add(bit, count[i][FIRST] + 1, 1);
		}
		out.println(res);
	}
	public int sum(int[] bit, int i) {
		int res = 0;
		while (i > 0) {
			res += bit[i];
			i -= (i & -i);
		}
		return res;
	}
	public void add(int[] bit, int i, int val) {
		while (i < bit.length) {
			bit[i] += val;
			i += (i & -i);
		}
	}
	public int[][] getCount(final int N, int[] a) {
		HashMap<Integer, Integer> index = new HashMap<Integer, Integer>();
		int[][] count = new int[N][2];
		for (int i = 0; i < N; i++) {
			Integer last = index.get(a[i]);
			if (last == null) {
				last = i;
			}
			count[i][LAST] = count[last][LAST] + 1;
			index.put(a[i], i);
		}
		for (int i = 0; i < N; i++) {
			count[i][FIRST] = count[index.get(a[i])][LAST] - count[i][LAST] + 1;
		}
		return count;
	}
}

class OutputWriter {
	private PrintWriter output;
	public OutputWriter() {
		this(System.out);
	}
	public OutputWriter(OutputStream out) {
		output = new PrintWriter(out);
	}
	public OutputWriter(Writer writer) {
		output = new PrintWriter(writer);
	}

	public void println(Object o) {
		output.println(o);
	}

	public void close() {
		output.close();
	}
}

class InputReader {
	private BufferedReader input;
	private StringTokenizer line = new StringTokenizer("");
	public InputReader() {
		this(System.in);
	}
	public InputReader(InputStream in) {
		input = new BufferedReader(new InputStreamReader(in));
	}
	public InputReader(String s) {
		try {
			input = new BufferedReader(new FileReader(s));
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}
	public void fill() {
		try {
			if(!line.hasMoreTokens()) line = new StringTokenizer(input.readLine());
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public int readInt() {
		fill();
		return Integer.parseInt(line.nextToken());
	}
}

