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 Template implements Runnable {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    void init() throws FileNotFoundException {
        try {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        } catch (Exception e) {
            String filename = "";
            if (filename.isEmpty()) {
                in = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(System.out);
            } else {
                in = new BufferedReader(new FileReader(filename + ".in"));
                out = new PrintWriter(filename + ".out");
            }
        }
    }

    String readString() {
        while (!tok.hasMoreTokens()) {
            try {
                tok = new StringTokenizer(in.readLine(), " :");
            } catch (Exception e) {
                return null;
            }
        }
        return tok.nextToken();
    }

    int readInt() {
        return Integer.parseInt(readString());
    }

    int[] readIntArray(int size) {
        int[] res = new int[size];
        for (int i = 0; i < size; i++) {
            res[i] = readInt();
        }
        return res;
    }

    long readLong() {
        return Long.parseLong(readString());
    }

    double readDouble() {
        return Double.parseDouble(readString());
    }

    <T> List<T>[] createGraphList(int size) {
        List<T>[] list = new List[size];
        for (int i = 0; i < size; i++) {
            list[i] = new ArrayList<>();
        }
        return list;
    }

    class Graph {
        int[][] graph;
        List<Integer>[] temp;
        int n;

        Graph(int n) {
            this.n = n;
            graph = new int[n][];
            temp = createGraphList(n);
        }

        void add(int x, int y) {
            temp[x].add(y);
            temp[y].add(x);
        }

        void addDir(int x, int y) {
            temp[x].add(y);
        }

        void build() {
            for (int i = 0; i < n; i++) {
                graph[i] = new int[temp[i].size()];
                for (int j = 0; j < temp[i].size(); j++) graph[i][j] = temp[i].get(j);
            }
        }

        void buildWithUniq() {
            for (int i = 0; i < n; i++) {
                HashSet<Integer> uniqSet = new HashSet<>();
                uniqSet.addAll(temp[i]);
                graph[i] = new int[uniqSet.size()];
                int index = 0;
                for (int x : uniqSet) {
                    graph[i][index++] = x;
                }
            }
        }
    }

    public static void main(String[] args) {
        new Template().run();
    }

    long timeBegin, timeEnd;

    void time() {
        timeEnd = System.currentTimeMillis();
        System.err.println("Time = " + (timeEnd - timeBegin));
    }

    long memoryTotal, memoryFree;

    void memory() {
        memoryFree = Runtime.getRuntime().freeMemory();
        System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
                + " KB");
    }

    public void run() {
        try {
            timeBegin = System.currentTimeMillis();
            memoryTotal = Runtime.getRuntime().freeMemory();
            init();
            solve();
            out.close();
            if (System.getProperty("ONLINE_JUDGE") == null) {
                time();
                memory();
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    class FenwikTree {
        long[] dataMul;
        long[] dataAdd;

        int n;

        FenwikTree(int n) {
            dataAdd = new long[n];
            dataMul = new long[n];
            this.n = n;
        }

        void intern(int at, long mul, long add) {
            while (at < n) {
                dataMul[at] += mul;
                dataAdd[at] += add;
                at |= at + 1;
            }
        }

        void update(int l, int r, long by) {
            if (l > r) return;
            intern(l, by, -by * (l - 1));
            intern(r, -by, by * r);
        }

        long get(int a) {
            if (a == 0) return q(0);
            return q(a) - q(a - 1);
        }

        long q(int at) {
            long mul = 0;
            long add = 0;
            long start = at;
            while (at >= 0) {
                mul += dataMul[at];
                add += dataAdd[at];
                at = (at & (at + 1)) - 1;
            }
            return start * mul + add;
        }
    }

    class Counter {
        HashMap<Integer, Integer> map = new HashMap<>();

        void add(int x) {
            map.put(x, get(x) + 1);
        }

        void remove(int x) {
            map.put(x, get(x) - 1);
        }

        int get(int x) {
            Integer v = map.get(x);
            return v == null ? 0 : v;
        }
    }

    private void solve() throws IOException {
        int n = readInt();
        int[] a = readIntArray(n);
        int size = n + 100;
        FenwikTree fenwikTree = new FenwikTree(size);
        Counter right = new Counter();
        for (int i = a.length-1; i >= 0; i--) {
            right.add(a[i]);
            fenwikTree.update(right.get(a[i]) + 1, size - 1, 1);
        }
        long ans = 0;
        Counter left = new Counter();
        for (int i = 0; i < a.length; i++) {
            fenwikTree.update(right.get(a[i]) + 1, size - 1, -1);
            right.remove(a[i]);
            left.add(a[i]);

            ans += fenwikTree.get(left.get(a[i]));
        }
        out.println(ans);

    }

}