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 D459 
{

	public static int mod = 1000000007;
	public static long INF = (1L << 60);
	static FastScanner2 in = new FastScanner2();
	static OutputWriter out = new OutputWriter(System.out);
 
	static int n;
	static int[] a;
	static int[] tree;
	
	public static void update(int node,int start,int end,int idx,int val)
	{
		if(start==end)
		{
			tree[node]+=val;
			return;
		}
		else
		{
			int mid=(start+end)>>1;
			if(idx<=mid)
			{
				update(2*node, start, mid, idx, val);
			}
			else
			{
				update(2*node+1, mid+1, end, idx, val);
			}
			tree[node]=tree[2*node]+tree[2*node+1];
		}
	}
	
	public static int query(int node,int start,int end,int l,int r)
	{
		if(start>r || end<l)
		{
			return 0;
		}
		if(start>=l && end<=r)
		{
			return tree[node];
		}
		int mid=(start+end)>>1;
		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) 
	{

		n=in.nextInt();
		a=new int[n+1];
		for(int i=1;i<=n;i++)
		{
			a[i]=in.nextInt();
		}
		tree=new int[4*n+100];
		int[] b=a.clone();
		Arrays.sort(b);
		Map<Integer, Integer> map=new HashMap<>();
		int count=1;
		map.put(b[1], count++);
		for(int i=2;i<=n;i++)
		{
			if(b[i]!=b[i-1])
			{
				map.put(b[i], count++);
			}
		}
		for(int i=1;i<=n;i++)
		{
			a[i]=map.get(a[i]);
		}
		int[] freqr=new int[n+1];
		int[] freqf=new int[n+1];
		for(int i=1;i<=n;i++)
		{
			freqr[a[i]]++;
			update(1, 1, n, freqr[a[i]], 1);
		}
		long ans=0;
		for(int i=1;i<=n;i++)
		{
			freqf[a[i]]++;
			update(1, 1, n, freqr[a[i]], -1);
			freqr[a[i]]--;
			ans+=(long)query(1, 1, n, 1, freqf[a[i]]-1);
			tr(ans);
		}
		out.println(ans);
		out.close();

	}
	

	static class FastScanner2 
	{
		private byte[] buf = new byte[1024];
		private int curChar;
		private int snumChars;

		public int read() 
		{
			if (snumChars == -1)
				throw new InputMismatchException();
			if (curChar >= snumChars) 
			{
				curChar = 0;
				try 
				{
					snumChars = System.in.read(buf);
				} catch (IOException e) 
				{
					throw new InputMismatchException();
				}
				if (snumChars <= 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 int[] nextIntArray(int n)
		{
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) 
			{
				arr[i] = nextInt();
			}
			return arr;
		}

		public long[] nextLongArray(int n)
		{
			long[] arr = new long[n];
			for (int i = 0; i < n; i++) 
			{
				arr[i] = nextLong();
			}
			return arr;
		}

		private 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;
		}
	}


	static class OutputWriter 
	{
		private final PrintWriter writer;

		public OutputWriter(OutputStream outputStream) 
		{
			writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
		}

		public OutputWriter(Writer writer)
		{
			this.writer = new PrintWriter(writer);
		}

		public void print(Object... objects)
		{
			for (int i = 0; i < objects.length; i++) 
			{
				if (i != 0)
					writer.print(' ');
				writer.print(objects[i]);
			}
		}

		public void println(Object... objects)
		{
			print(objects);
			writer.println();
		}

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

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