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.util.*;

public class Main{
    final boolean isFileIO = false; 
    BufferedReader in;
    PrintWriter out;
    StringTokenizer st = new StringTokenizer("");
    String delim = " ";

    public static void main(String[] args) throws IOException {
        Main m = new Main();
        m.initIO();
        m.solve();
        m.in.close();
        m.out.close();
    }

    public void initIO() throws IOException {
        if(!isFileIO) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt"); 
        }
    }
  
    String nextToken() throws IOException {
        if(!st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken(delim);
    }
 
    String readLine() throws IOException {
        return in.readLine();
    }

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

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

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

    class SegmentTree {
        public int[] b;
        public int n;
        public SegmentTree(int[] a) {
            n = Integer.highestOneBit(a.length * 2);
            b = new int[2 * n - 1];
            for(int i = 0; i < a.length; i++) {
                b[n - 1 + i] = a[i];
            }
            for(int i = n - 2; i >= 0; i--) {
                b[i] = b[2 * i + 1] + b[2 * i + 2];
            }
        }
        public int getValue(int pos) {
            return b[n - 1 + pos];
        }
        public void update(int v, int l, int r, int pos, int val) {
            int mid = (r + l) / 2;
            if(l == r) {
                b[n - 1 + l] = val;
                return;
            }
            if(pos <= mid) {
                update(2 * v + 1, l, mid, pos, val);
            } else {
                update(2 * v + 2, mid + 1, r, pos, val);
            }
            b[v] = b[v * 2 + 1] + b[v * 2 + 2];
        }
        public int sum(int v, int l, int r, int from, int to) {
            if(from > to)
                return 0;
            if(l == from && r == to) {
                return b[v];
            }
            int mid = (r + l) / 2;
            return sum(2 * v + 1, l, mid, from, Math.min(to, mid)) + sum(2 * v + 2, mid + 1, r, Math.max(from, mid + 1), to);
        }
    }


    void solve() throws IOException {
        int n = nextInt();
        int[] a = new int[n];
        for(int i = 0; i < n; i++)
            a[i] = nextInt();
        int[] b = new int[n];
        SegmentTree st = new SegmentTree(b);
        HashMap<Integer, Integer> hashLeft = new HashMap();
        HashMap<Integer, Integer> hashRight = new HashMap();
        long ans = 0;
        for(int i = n - 1; i >= 1; i--) {
            int val = hashRight.containsKey(a[i]) ? hashRight.get(a[i]) : 0;
            hashRight.put(a[i], val + 1);
            st.update(0, 0, st.n - 1, val + 1, st.getValue(val + 1) + 1);
        }
        for(int i = 0; i < n - 1; i++) {
            int val = hashLeft.containsKey(a[i]) ? hashLeft.get(a[i]) : 0;
            long cnt = st.sum(0, 0, st.n - 1, 0, val);
            ans += cnt;
            hashLeft.put(a[i], val + 1);
            int valRight = hashRight.get(a[i + 1]);
            st.update(0, 0, st.n - 1, valRight, st.getValue(valRight) - 1);
            hashRight.put(a[i + 1], hashRight.get(a[i + 1]) - 1);
        }
        out.println(ans);
    }
}
