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.security.KeyStore.Entry;
import java.util.*;

public class TestClass {
	private static InputStream stream;
	private static byte[] buf = new byte[1024];
	private static int curChar;
	private static int numChars;
	private static SpaceCharFilter filter;
	private static PrintWriter pw;
	
	static int a[];
	public static void Update(int x,int y,int index,int counter){
		if(x>=y){
			a[counter]+=1;
			return ;
		}
		int mid = (x+y)>>1;
		if(mid>=index)
			Update(x,mid,index,2*counter+1);
		else
			Update(mid+1,y,index,2*counter+2);
		a[counter] = a[2*counter+1]+a[2*counter+2];
	}
	public static int getSum(int x,int y,int p,int q,int counter){
		if(x>y||p>q||x>q||p>y)
			return 0;
		if(p<=x&&q>=y)
			return a[counter];
		int mid = (x+y)>>1;
		return getSum(x,mid,p,q,2*counter+1)+getSum(mid+1,y,p,q,2*counter+2);
	}
	private static void soln() {
		int n = nI();
		int arr[] = new int[n];
		for(int i=0;i<n;i++)
			arr[i] = nI();
		int min[] = new int[n],max[] = new int[n];
		HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
		for(int i=0;i<n;i++){
			if(map.containsKey(arr[i])){
				min[i] = map.get(arr[i])+1;
				map.put(arr[i], min[i]);
			}
			else{
				min[i] = 1;
				map.put(arr[i], min[i]);
			}
		}
		map.clear();
		for(int i=n-1;i>=0;i--){
			if(map.containsKey(arr[i])){
				max[i] = map.get(arr[i])+1;
				map.put(arr[i], max[i]);
			}
			else{
				max[i] = 1;
				map.put(arr[i], max[i]);
			}
		}
		a = new int[10*n];
		long ans = 0;
		Update(0,n-1,max[n-1],0);
		for(int i=n-2;i>=0;i--){
			ans+=getSum(0,n-1,0,min[i]-1,0);
			Update(0,n-1,max[i],0);
		}
		pw.println(ans);
	}

	public static void main(String[] args) {
		InputReader(System.in);
		pw = new PrintWriter(System.out);
		soln();
		pw.close();
	}

	// To Get Input
	// Some Buffer Methods
	public static void InputReader(InputStream stream1) {
		stream = stream1;
	}

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

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

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

	private static int nI() {
		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;
	}

	private static long nL() {
		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;
	}

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

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

	private static boolean isSpaceChar(int c) {
		if (filter != null)
			return filter.isSpaceChar(c);
		return isWhitespace(c);
	}

	private interface SpaceCharFilter {
		public boolean isSpaceChar(int ch);
	}
}