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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		new Thread(null, new Runnable() {
			public void run() {
				try {
					long prevTime = System.currentTimeMillis();
					new Main().run();
					System.err.println("Total time: "
							+ (System.currentTimeMillis() - prevTime) + " ms");
					System.err.println("Memory status: " + memoryStatus());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}, "1", 1L << 24).start();
	}

	void run() throws IOException {
		in = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		Object o = solve();
		if (o != null)
			out.println(o);
		out.close();
		in.close();
	}

	private Object solve() throws IOException {
		int n = ni();
		int[] arr = nia(n);
		int MAX = compress(arr);
		
		int[] pref = go(arr, 0, n, 1, MAX);
		int[] suff = go(arr, n - 1, -1,-1, MAX);
		int[] count = new int[n+1];
		for(int a : pref)
			count[a]++;
		
		BIT tree = new BIT(n+1);
		for(int a : pref)
			tree.add(a, 1);
		
		long res = 0;
		for (int i = n - 1; i > 0; i--) {
			tree.add(pref[i], -1);
			res += tree.sum(suff[i]+1,n+1);
		}
		return res;
	}

	private int[] go(int[] arr, int lo, int hi, int diff, int MAX) {
		int n = arr.length;
		int[] res = new int[n];
		
		int[] multiset = new int[MAX+1];
		for(int i =lo; i != hi;i+= diff){
			multiset[arr[i]]++;
			res[i] = multiset[arr[i]];
		}
		return res;
	}

	class Pair implements Comparable<Pair>{
		int val;
		int index;
		
		public Pair(int val, int index) {
			this.val = val;
			this.index = index;
		}

		@Override
		public int compareTo(Pair o) {
			return this.val - o.val;
		}
	}
	
	int compress(int[] arr) {
		int n = arr.length;
		
		Pair[] ps = new Pair[n];
		for(int i =0;i<n;i++)
			ps[i] = new Pair(arr[i],i);
		Arrays.sort(ps);

		int lo =0; 
		int index =0;
		while(lo < arr.length){
			int hi= lo+1;
			while(hi < ps.length && ps[hi].val == ps[lo].val)
				hi++;
			for(int i =lo;i<hi;i++)
				arr[ps[i].index] = index;
			index++;
			lo = hi;
		}
		return index;
	}
	
	public class BIT {
		int[] vs;
		
		BIT(int n){
			vs = new int[n+1];
		}
		
		void add(int k, int a) {
			for(int i = k+1; i<vs.length; i+=i & -i){
				vs[i] +=a;	
			}
		}
		
		int sum(int s, int t){
			if(s > 0)
				return sum(0,t) -sum(0,s);	
			
			int res = 0;
			for(int i = t; i>0; i -= i & -i)
				res+=vs[i];							
			return res;
		}
		
		int get(int k) {
			int p = Integer.highestOneBit(vs.length - 1);
			for (int q = p; q > 0; q >>= 1, p |= q) {
				if (p >= vs.length || k < vs[p])  
					p ^= q;
				else
					k -= vs[p];
			}
		 return p;
		}
	}
	BufferedReader in;
	PrintWriter out;
	StringTokenizer strTok = new StringTokenizer("");

	String nextToken() throws IOException {
		while (!strTok.hasMoreTokens())
			strTok = new StringTokenizer(in.readLine());
		return strTok.nextToken();
	}

	int ni() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nl() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nd() throws IOException {
		return Double.parseDouble(nextToken());
	}

	int[] nia(int size) throws IOException {
		int[] ret = new int[size];
		for (int i = 0; i < size; i++)
			ret[i] = ni();
		return ret;
	}

	long[] nla(int size) throws IOException {
		long[] ret = new long[size];
		for (int i = 0; i < size; i++)
			ret[i] = nl();
		return ret;
	}

	double[] nda(int size) throws IOException {
		double[] ret = new double[size];
		for (int i = 0; i < size; i++)
			ret[i] = nd();
		return ret;
	}

	String nextLine() throws IOException {
		strTok = new StringTokenizer("");
		return in.readLine();
	}

	boolean EOF() throws IOException {
		while (!strTok.hasMoreTokens()) {
			String s = in.readLine();
			if (s == null)
				return true;
			strTok = new StringTokenizer(s);
		}
		return false;
	}

	void printRepeat(String s, int count) {
		for (int i = 0; i < count; i++)
			out.print(s);
	}

	void printArray(int[] array) {
		for (int i = 0; i < array.length; i++) {
			if (i > 0)
				out.print(' ');
			out.print(array[i]);
		}
		out.println();
	}

	void printArray(long[] array) {
		for (int i = 0; i < array.length; i++) {
			if (i > 0)
				out.print(' ');
			out.print(array[i]);
		}
		out.println();
	}

	void printArray(double[] array) {
		for (int i = 0; i < array.length; i++) {
			if (i > 0)
				out.print(' ');
			out.print(array[i]);
		}
		out.println();
	}

	void printArray(double[] array, String spec) {
		for (int i = 0; i < array.length; i++) {
			if (i > 0)
				out.print(' ');
			out.printf(Locale.US, spec, array[i]);
		}
		out.println();
	}

	void printArray(Object[] array) {
		boolean blank = false;
		for (Object x : array) {
			if (blank)
				out.print(' ');
			else
				blank = true;
			out.print(x);
		}
		out.println();
	}

	@SuppressWarnings("rawtypes")
	void printCollection(Collection collection) {
		boolean blank = false;
		for (Object x : collection) {
			if (blank)
				out.print(' ');
			else
				blank = true;
			out.print(x);
		}
		out.println();
	}

	static String memoryStatus() {
		return (Runtime.getRuntime().totalMemory()
				- Runtime.getRuntime().freeMemory() >> 20)
				+ "/" + (Runtime.getRuntime().totalMemory() >> 20) + " MB";
	}

	public void pln() {
		out.println();
	}

	public void pln(int arg) {
		out.println(arg);
	}

	public void pln(long arg) {
		out.println(arg);
	}

	public void pln(double arg) {
		out.println(arg);
	}

	public void pln(String arg) {
		out.println(arg);
	}

	public void pln(boolean arg) {
		out.println(arg);
	}

	public void pln(char arg) {
		out.println(arg);
	}

	public void pln(float arg) {
		out.println(arg);
	}

	public void pln(Object arg) {
		out.println(arg);
	}

	public void p(int arg) {
		out.print(arg);
	}

	public void p(long arg) {
		out.print(arg);
	}

	public void p(double arg) {
		out.print(arg);
	}

	public void p(String arg) {
		out.print(arg);
	}

	public void p(boolean arg) {
		out.print(arg);
	}

	public void p(char arg) {
		out.print(arg);
	}

	public void p(float arg) {
		out.print(arg);
	}

	public void p(Object arg) {
		out.print(arg);
	}

}