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.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		Scanner in = new Scanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		Default solver = new Default();
		solver.solve(1, in, out);
		out.close();
	}
}

class Default {

    static final int N = 1000005;

    public static HashMap<Integer, Integer> H = new HashMap<Integer, Integer>();


    int [] a = new int[N], s = new int[N], l = new int [N], r = new int [N];
    int n;

    int lb(int x){
        return x & -x;
    }

    void add(int x){
        for (int i=x;i<=n;i+=lb(i))s[i]++;
    }
    int sum(int x){
        int z = 0; for (int i=x;i>0;i-=lb(i)) z += s[i];
        return z;
    }


    public void solve(int testNumber, Scanner in, PrintWriter out) {
        long ans = 0; n = in.nextInt();
        for (int i=1;i<=n;++i){
            a[i] = in.nextInt();
            H.put(a[i], (H.containsKey(a[i]) ? H.get(a[i]) : 0) +1) ;
            l[i] = H.get(a[i]);
        }
        
        H.clear();
        for (int i=n;i>0;--i){
            H.put(a[i], (H.containsKey(a[i]) ? H.get(a[i]) : 0) +1) ;
            r[i] = H.get(a[i]);
        }
        
        for (int i=1;i<=n;++i){
            ans += sum(n)-sum(r[i]);
            add(l[i]);
        }
        
        
        out.println(ans);
    }
}
