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.*;
import java.io.*;
import java.math.*;
 
public class Main {
    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(System.out);
        //Scanner sc = new Scanner();
        Reader in = new Reader();
        Main solver = new Main();
        solver.solve(out, in);
        out.flush();
        out.close();
 
    }
 
    //<>
    
    static int maxn = (int)1e5*2;
    static long mod=(int)1e9+7;
    static int n,t,m,k;
    
    static Map<Integer,Integer> map;
    static int[] pre,suf,arr,freq,BIT;
    
    void solve(PrintWriter out, Reader in) throws IOException{   
        n = in.nextInt();
        
        BIT = new int[n+1];
        arr = new int[n+1];
        pre = new int[n+1];
        suf = new int[n+1];
        map = new HashMap<>();
        int cnt=0;
        
        int temp;
        for(int i=1;i<=n;i++){
            temp = in.nextInt();
            map.put(temp,map.getOrDefault(temp,++cnt));
            arr[i] = map.get(temp);
        }
        
        freq = new int[n+1];
        for(int i=1;i<=n;i++) {
            pre[i] = 1 + freq[arr[i]];
            freq[arr[i]]++;
        }
        
        freq = new int[n+1];
        for(int i=n;i>=1;i--){
            suf[i] = 1 + freq[arr[i]];
            freq[arr[i]]++;
        }
        
        long ans=0;
        for(int i=1;i<=n;i++){
            update(suf[i],1);
        }
        
        for(int i=1;i<n;i++){
            update(suf[i],-1);
            ans += query(pre[i]-1);
        }
        
        out.println(ans);
    }
    
    static void update(int idx,int v){
        for(;idx<=n;idx+=(idx&-idx))
            BIT[idx]+=v;
    }
    
    static int query(int idx){
        int sum=0;
        for(;idx>=1;idx-=(idx&-idx))
            sum += BIT[idx];
            
        return sum;
    }
    
    static class Edge implements Comparable<Edge> {
        int d,w;
        
        Edge(int d,int w){
            this.d = d;
            this.w = w;
        }
        
        
        public int compareTo(Edge o){
            return this.w - o.w;
        }
    }
    
    
    static class Reader {
 
        private InputStream mIs;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
 
        public Reader() {
            this(System.in);
        }
 
        public Reader(InputStream is) {
            mIs = is;
        }
 
        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
 
        }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = mIs.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }
 
        public String nextLine() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }
 
        public String next() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }
 
        double nextDouble()
        {
            return Double.parseDouble(next());
        }
 
        public long nextLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
 
        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
 
        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
 
        public boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }
 
    }
}