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.HashMap;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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 {
		int n;
		PrintWriter out;
		InputReader in;

		public void solve(int testNumber, InputReader in, PrintWriter out) {
			int t, i, j, tt, k;
			this.out = out;
			this.in = in;
			n = in.nextInt();
			int a[] = new int[n];
			for (i = 0; i < n; i++) {
				a[i] = in.nextInt();
			}
			int x[] = new int[n + 1];
			SegmentTree sg = new SegmentTree();
			sg.build(x);
			HashMap<Integer, Integer> hp = new HashMap<>();
			for (i = n - 1; i >= 0; i--) {
				if (hp.containsKey(a[i]))
					hp.put(a[i], hp.get(a[i]) + 1);
				else
					hp.put(a[i], 1);
				x[i] = hp.get(a[i]);
			}
			for (i = 0; i < n; i++) {
				long yp = sg.query(x[i], x[i] + 1) + 1;
				sg.updateTreeNode(x[i], yp);
			}
			long ss = 0;
			hp.clear();
			for (i = 0; i < n; i++) {
				if (hp.containsKey(a[i]))
					hp.put(a[i], hp.get(a[i]) + 1);
				else
					hp.put(a[i], 1);
				long yp = sg.query(x[i], x[i] + 1);
				yp = Math.max(yp - 1, 0);
				sg.updateTreeNode(x[i], yp);
				ss += sg.query(1, hp.get(a[i]));
			}
			pn(ss);
		}

		void pn(long zx) {
			out.println(zx);
		}

		class SegmentTree {
			long[] tree;
			int n;

			void build(int arr[]) {
				n = arr.length;
				tree = new long[2 * n + 1];
				for (int i = 0; i < n; i++) {
					tree[n + i] = arr[i];
				}
				for (int i = n - 1; i > 0; i--) {
					tree[i] = tree[2 * i] + tree[2 * i + 1];
				}
			}

			void updateTreeNode(int p, long val) {
				tree[p + n] = val;
				p += n;
				for (int i = p; i > 1; i >>= 1) {
					tree[i >> 1] = tree[i] + tree[i ^ 1];
				}
			}

			long query(int l, int r) {
				long ret = 0;
				for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
					if ((l & 1) == 1)
						ret += tree[l++];
					if ((r & 1) == 1)
						ret += tree[--r];
				}
				return ret;
			}

		}

	}

	static class InputReader {
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		public int read() {
			if (numChars == -1) {
				throw new UnknownError();
			}
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new UnknownError();
				}
				if (numChars <= 0) {
					return -1;
				}
			}
			return buf[curChar++];
		}

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

		public String next() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuffer res = new StringBuffer();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));

			return res.toString();
		}

		private boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

	}
}

