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.*;
import java.math.*;
import java.util.*;

import static java.util.Arrays.fill;
import static java.lang.Math.*;
import static java.util.Arrays.sort;
import static java.util.Collections.sort;

public class PashmakandParmidaproblem {
	static int mod = 1000000007;
	static InputReader in = new InputReader(System.in);
	static PrintWriter out = new PrintWriter(System.out);

	static int[] tree;
	
	static void update(int node,int start,int end,int idx)
	{
		if(start==end)
		{
			tree[node]++;
			//tr("updated node is "+idx);
			return;
		}
		int mid=(start+end)/2;
		if(mid>=idx)
			update(2*node, start, mid, idx);
		else
			update(2*node+1, mid+1, end, idx);
		tree[node]=tree[2*node]+tree[2*node+1];
	}
	
	static int query(int node,int start,int end,int l,int r)
	{
		if(start>end||start>r||end<l)
		{
			return 0;
		}
		if(start>=l&&end<=r)
		{
			return tree[node];
		}
		int mid=(start+end)/2;
		int p1=query(2*node, start, mid, l, r);
		int p2=query(2*node+1, mid+1, end, l, r);
		return (p1+p2);
	}
	
	public static void main(String[] args) {

		int n=in.nextInt();
		int[] a=new int[n];
		Map<Integer, Integer> fmap=new HashMap<>();
		Map<Integer, Integer> rmap=new HashMap<>();
		tree=new int[4*n+1];
		for(int i=0;i<n;i++)
		{
			a[i]=in.nextInt();
			if(fmap.containsKey(a[i]))
				fmap.put(a[i], fmap.get(a[i])+1);
			else
				fmap.put(a[i], 1);
		}
		long ans=0;
		for(int i=n-1;i>=0;i--)
		{
			//tr(1+" "+(fmap.get(a[i])-1));
			ans+=query(1, 1, n, 1, fmap.get(a[i])-1);
			if(rmap.containsKey(a[i]))
				rmap.put(a[i], rmap.get(a[i])+1);
			else
				rmap.put(a[i], 1);
			fmap.put(a[i], fmap.get(a[i])-1);
			update(1, 1, n, rmap.get(a[i]));
		}
		out.println(ans);
		out.close();

	}

	static class InputReader {
		private final InputStream stream;
		private final byte[] buf = new byte[8192];
		private int curChar, snumChars;

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

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

		public int nextInt() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public long nextLong() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public String readString() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public String nextLine() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndOfLine(c));
			return res.toString();
		}

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

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

	private static void tr(Object... o) {
		if (!(System.getProperty("ONLINE_JUDGE") != null))
			System.out.println(Arrays.deepToString(o));
	}
}


