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.StreamTokenizer;
import java.util.HashMap;
import java.util.Scanner;


public class ProblemD {

	private static class Node {
		int left;
		int right;
		int sum;
		Node leftSon;
		Node rightSon;
		
		public Node(int l, int r, int q, Node ls, Node rs){
			left = l;
			right = r;
			sum = q;
			leftSon = ls;
			rightSon = rs;
		}
	}
	
	private static int getSum(int l, int r, Node root){
		if (l == root.left && r == root.right) {
			return root.sum;
		}
		int middle = (root.left + root.right) / 2;
		if (r <= middle) return getSum(l, r, root.leftSon);
		if (l > middle) return getSum(l, r, root.rightSon);
		return getSum(l, middle, root.leftSon) + getSum(middle+1, r, root.rightSon);
	}
	
	private static void addValue(int x, Node root){
		if (root == null){
			return;
		}
		root.sum ++;
		int middle = (root.left + root.right) / 2;
		if (x <= middle){
			addValue(x, root.leftSon);
		}
		else {
			addValue(x, root.rightSon);
		}
	}
	
	private static void removeValue(int x, Node root){
		if (root == null){
			return;
		}
		root.sum --;
		int middle = (root.left + root.right) / 2;
		if (x <= middle){
			removeValue(x, root.leftSon);
		}
		else {
			removeValue(x, root.rightSon);
		}
	}
	
	private static void buildTree(Node root, int l, int r){
		if (r == l)
			return;
		int middle = (r + l) / 2;
		root.leftSon = new Node(l, middle, 0, null, null);
		root.rightSon = new Node(middle + 1, r, 0, null, null);
		buildTree(root.leftSon, l, middle);
		buildTree(root.rightSon, middle + 1, r);
	}
	
	static StreamTokenizer st;
	static int nextInt() throws IOException
	   {
	      st.nextToken();
	      return (int)st.nval;
	   }
	
	public static void main(String[] args) throws IOException {
		
		st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		
		int n = nextInt();
		int[] a = new int[n];
		HashMap<Integer, Integer> count1 = new HashMap<Integer, Integer>();
		HashMap<Integer, Integer> count2 = new HashMap<Integer, Integer>();
		int[] index1 = new int[n];
		int[] index2 = new int[n];
		for(int i = 0; i < n; i++){
			a[i] = nextInt();
			int c = count1.containsKey(a[i]) ? count1.get(a[i]) : 0;
			index1[i] = c + 1;
			count1.put(a[i], c + 1);
		}
		
		Node root = new Node(0, n, 0, null, null);
		buildTree(root, 0, n);
		
		for(int i = n - 1; i >= 0; i--){
			int c = count2.containsKey(a[i]) ? count2.get(a[i]) : 0;
			index2[i] = c + 1;
			count2.put(a[i], c + 1);
			addValue(c + 1, root);
		}
		
		long answer = 0;
		
		for(int i = 0; i < n; i++){
			removeValue(index2[i], root);
			if (index1[i] > 1){
				answer += getSum(1, index1[i] - 1, root);
				}
		}
		
		System.out.println(answer);
	}

}
