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.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class P459D
{
	public static void main(String[] args)
	{
		FastScanner scan = new FastScanner();
		int n = scan.nextInt();
		int[] arr = new int[n];
		for (int i = 0; i < n; i++)
			arr[i] = scan.nextInt();
		HashMap<Integer, Integer> map = new HashMap<>();
		int[] l2r = new int[n];
		for (int i = 0; i < n; i++)
		{
			Integer freq = map.get(arr[i]);
			if (freq == null)
			{
				map.put(arr[i], 1);
				l2r[i] = 1;
			}
			else
			{
				map.put(arr[i], freq+1);
				l2r[i] = freq+1;
			}
		}
		map = new HashMap<>();
		int[] r2l = new int[n];
		for (int i = n-1; i >= 0; i--)
		{
			Integer freq = map.get(arr[i]);
			if (freq == null)
			{
				map.put(arr[i], 1);
				r2l[i] = 1;
			}
			else
			{
				map.put(arr[i], freq+1);
				r2l[i] = freq+1;
			}
		}
		PriorityQueue<Num> pq1 = new PriorityQueue<>();
		for (int i = 0; i < n; i++)
			pq1.add(new Num(i+1, l2r[i]));
		PriorityQueue<Num> pq2 = new PriorityQueue<>();
		for (int i = 0; i < n; i++)
			pq2.add(new Num(i+1, r2l[i]));
		long total = 0;
		BIT valid = new BIT(n);
		while (!pq1.isEmpty())
		{
			Num num = pq1.poll();
			while (pq2.peek().value < num.value)
			{
				Num other = pq2.poll();
				valid.add(other.index, 1);
			}
			total += valid.sum(num.index+1, n);
		}
		System.out.println(total);
	}
	static class Num implements Comparable<Num>
	{
		int index, value;
		Num(int i, int v)
		{
			index = i;
			value = v;
		}
		public int compareTo(Num n)
		{
			return value - n.value;
		}
	}
	static class BIT
	{
		int[] arr;
		BIT(int n)
		{
			arr = new int[n+1];
		}
		void add(int index, int val)
		{
			while (index < arr.length)
			{
				arr[index] += val;
				index += index & -index;
			}
		}
		int sum(int l, int r)
		{
			return sum(r)-sum(l-1);
		}
		int sum(int index)
		{
			int sum = 0;
			while (index > 0)
			{
				sum += arr[index];
				index -= index & -index;
			}
			return sum;
		}
	}
	static class FastScanner
	{
		BufferedReader br;
		StringTokenizer st;

		public FastScanner()
		{
			try
			{
				br = new BufferedReader(new InputStreamReader(System.in));
				st = new StringTokenizer(br.readLine());
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}

		public String next()
		{
			if (st.hasMoreTokens())
				return st.nextToken();
			try
			{
				st = new StringTokenizer(br.readLine());
			} catch (Exception e)
			{
				e.printStackTrace();
			}
			return st.nextToken();
		}

		public int nextInt()
		{
			return Integer.parseInt(next());
		}

		public long nextLong()
		{
			return Long.parseLong(next());
		}

		public String nextLine()
		{
			String line = "";
			try
			{
				line = br.readLine();
			} catch (Exception e)
			{
				e.printStackTrace();
			}
			return line;
		}
	}
}
