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.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.InputMismatchException;

public class Main {

	public static void main(String[] args) {
		TaskD mSol = new TaskD();
		mSol.solve();
		mSol.flush();
		mSol.close();
	}

}

class TaskD {

	public FasterScanner mFScanner;
	public PrintWriter mOut;
	public int mN;
	public int mArray[];
	public int mCountBegin[];
	public int mCountLast[];
	public int mBit[];
	public int MAX;
	public HashMap<Integer, Integer> mHashMapLast;

	public TaskD() {
		mFScanner = new FasterScanner();
		mOut = new PrintWriter(System.out);
	}

	public void solve() {
		int i;
		int index;
		int size;

		mHashMapLast = new HashMap<>();

		mN = mFScanner.nextInt();
		mArray = new int[mN];
		mCountBegin = new int[mN];
		mCountLast = new int[mN];
		mBit = new int[mN + 10];
		MAX = mN + 5;
		
//		mOut.println(size);
//		mOut.flush();

		for (i = 0; i < mN; i++) {
			mArray[i] = mFScanner.nextInt();
			if (mHashMapLast.containsKey(mArray[i])) {
				index = mHashMapLast.get(mArray[i]);
				mCountBegin[i] = mCountBegin[index] + 1;
			} else {
				mCountBegin[i] = 1;
			}
			mHashMapLast.put(mArray[i], i);
		}

		mHashMapLast.clear();

		for (i = mN - 1; i >= 0; i--) {
			if (mHashMapLast.containsKey(mArray[i])) {
				index = mHashMapLast.get(mArray[i]);
				mCountLast[i] = mCountLast[index] + 1;
			} else {
				mCountLast[i] = 1;
			}
			mHashMapLast.put(mArray[i], i);
		}
		
		long ans = 0;
		int curVal;
		int temp;
		
		for (i = 1; i < mN; i++) {
			update(mCountLast[i], 1);
		}
		
		for (i = 0; i < mN - 1; i++) {
			curVal = mCountBegin[i];
			temp = query(curVal - 1);
			ans += temp;
			update(mCountLast[i + 1], -1);
		}
//		
		mOut.println(ans);

	}
	
	public void update(int idx, int val) {
		while (idx <= MAX) {
			mBit[idx] += val;
			idx += (idx & -idx);
		}
	}
	
	public int query(int idx) {
		int res = 0;
		
		while (idx > 0) {
			res += mBit[idx];
			idx -= (idx & -idx);
		}
		
		return res;
	}

	public void flush() {
		mOut.flush();
	}

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

class FasterScanner {
	private InputStream mIs;
	private byte[] buf = new byte[1024];
	private int curChar;
	private int numChars;

	public FasterScanner() {
		this(System.in);
	}

	public FasterScanner(InputStream is) {
		mIs = is;
	}

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

	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 String nextString() {
		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 long nextLong() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

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

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

}