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.io.StreamTokenizer;
import java.util.*;

public class D implements Runnable {
    int nextInt(StreamTokenizer in) throws Exception {
        in.nextToken();
        return (int) in.nval;
    }

    int[] segTree;
    int base;

    void inc(int v) {
        while (v != 0) {
            segTree[v]++;
            v = v >> 1;
        }
    }

    int sum(int l, int r) {
        return sum(1, 0, base - 1, l, r);
    }

    int sum(int v, int a, int b, int l, int r) {
        if (b < l || a > r) return 0;
        if (l <= a && b <= r) return segTree[v];
        int mid = (a + b) / 2;
        return sum(2 * v, a, mid, l, r) + sum(2 * v + 1, mid + 1, b, l, r);
    }

    @Override
    public void run() {
        try {
            //Scanner in = new Scanner(System.in);
            StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
            int n = nextInt(in);
            List<Num> b = new ArrayList<Num>();
            for (int i = 0; i < n; i++) {
                b.add(new Num(nextInt(in), i));
            }
            Collections.sort(b);

            int[] a = new int[n];
            int cur = 0;
            int trim = 0;
            while (cur < n) {
                int t = b.get(cur).v;
                while (cur < n && t == b.get(cur).v) {
                    a[b.get(cur++).i] = trim;
                }
                trim++;
            }

            int[] fi = new int[n];
            Map<Integer, Integer> prev = new HashMap<Integer, Integer>();
            for (int i = 0; i < n; i++) {
                Integer prevIndex = prev.get(a[i]);
                int prevValue = 0;
                if (prevIndex != null) {
                    prevValue = fi[prevIndex];
                }
                fi[i] = prevValue + 1;
                prev.put(a[i], i);
            }

            prev.clear();

            base = 1;
            while (n + 1 > base) {
                base *= 2;
            }
            segTree = new int[2 * base];

            int[] fj = new int[n];
            long res = 0;
            for (int j = n - 1; j > 0; j--) {
                Integer prevIndex = prev.get(a[j]);
                int prevValue = 0;
                if (prevIndex != null) {
                    prevValue = fj[prevIndex];
                }
                fj[j] = prevValue + 1;
                prev.put(a[j], j);

                inc(base + fj[j]);
                if (fi[j - 1] == 0) continue;
                res += sum(0, fi[j - 1] - 1);
            }

            System.out.println(res);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        new Thread(new D()).start();
    }

    private static class Num implements Comparable<Num> {
        int v;
        int i;

        private Num(int v, int i) {
            this.v = v;
            this.i = i;
        }

        @Override
        public int compareTo(Num o) {
            if (v < o.v) return -1;
            if (v > o.v) return 1;

            if (i < o.i) return -1;
            if (i > o.i) return 1;

            return 0;
        }
    }

}
