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

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);
		Task solver = new Task();
		solver.solve(1, in, out);
		out.close();
	}

	static class Task {

		public void solve(int testNumber, InputReader in, PrintWriter out) {

			HashMap<Integer, Integer> count = new HashMap<Integer, Integer>();

			int n = in.nextInt();
			int[] s = new int[n];
			for (int i = 0; i < n; i++) {
				s[i] = in.nextInt();
			}
			int[] a = new int[n];
			for (int i = 0; i < n; i++) {
				int c = s[i];
				if (count.keySet().contains(c)) {
					count.put(c, count.get(c) + 1);
				} else {
					count.put(c, 1);
				}
				a[i] = count.get(c);
			}
			int[] b = new int[n];
			count = new HashMap<Integer, Integer>();
			int a_max = 10000000;
			FenwickTree ft = new FenwickTree(a_max+1);
			for (int i = n - 1; i >= 0; i--) {
				int c = s[i];
				if (count.keySet().contains(c)) {
					count.put(c, count.get(c) + 1);
				} else {
					count.put(c, 1);
				}
				b[i] = count.get(c);
				ft.update(b[i], 1);
			}
			//System.out.println(Arrays.toString(a));
			// System.out.println(Arrays.toString(b));

			long ans = 0;

			for (int i = 0; i < n; i++) {
				ft.update(b[i], -1);
				ans += ft.rsq(1, a[i]-1);
			}
			out.println(ans);
		}

	}

	static class InputReader {
		public BufferedReader reader;
		public StringTokenizer st;

		public InputReader(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(stream), 32768);
			st = null;
		}

		public String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return st.nextToken();
		}

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

		public long nextLong() {
			return Long.parseLong(next());
		}

	}



	static class FenwickTree {
		private int[] ft;
		private int n;
		public FenwickTree(int n) {
			//We want to contains values up to n [1...n]
			this.n = n + 1;
			this.ft = new int[n + 1];
		}
		public void update(int k, int v) { //update value at index k by adjusting its value by v
			while (k < n) {
				this.ft[k] += v;
				k += LSOne(k);
			}

		}
		public int rsq(int b) {
			int ans = 0;
			while (b != 0) {
				ans += this.ft[b];
				b -= LSOne(b);
			}
			return ans;
		}
		public int rsq(int a, int b) {
			return rsq(b) - (a == 1 ? 0 : rsq(a - 1));
		}
		public int LSOne(int i) {
			return i & (-i);
		}
		public int freq(int b) {
			return rsq(b) - rsq(b - 1);
		}



	}
}