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 D459 
{
    static long tree[],arr[],a[];
    static void update(int st,int end,int ind,int val,int node)
    {
        if(st==end)
        {
            a[ind]+=val;
            tree[node]+=val;
        }
        else
        {
            int mid=(st+end)/2;
            if(st<=ind && ind<=mid)
                update(st,mid,ind,val,node*2 +1);
            else
                update(mid+1,end,ind,val,node*2 +2);
            tree[node]=tree[node*2 +1]+tree[node*2 +2];
        }
    }
    static long get(int st,int end,int l,int r,int node)
    {
        if(r<st || end <l)
        {
            return 0;
        }
        if(l <=st && end<=r)
        {
        
            return tree[node];
        }
        int mid=(st+end)/2;
        return get(st,mid,l,r,node*2 +1)+get(mid+1,end,l,r,node*2 +2);
    }
    public static void main (String args[]) throws IOException
    {
        Reader sc=new Reader();
        HashMap<Long,Integer> map=new HashMap<>();
        int n=sc.nextInt();
        a=new long[n];
        arr=new long[n];
        tree=new long[n<<2];
        for(int i=0;i<n;i++)
        {
            arr[i]=sc.nextInt();
        }
        for(int i=n-1;i>0;i--)
        {
            if(!map.containsKey(arr[i]))
            {
                map.put(arr[i],1);
                update(0,n-1,1,1,0);
            }
            else
            {
                int num=map.get(arr[i]);
                map.put(arr[i],num+1);
                update(0,n-1,num+1,1,0);
            }
        }
        /*for(int i=0;i<(n<<2);i++)
            {
                System.out.println(tree[i]+" "+i);
            }*/
        HashMap<Long,Integer> cou=new HashMap<>();
        cou.put(arr[0],1);
        long count=0;
        if(n==1)
            System.out.println(0);
        else
        {
            for(int i=1;i<n-1;i++)
            {
                if(!cou.containsKey(arr[i]))
                {
                    cou.put(arr[i],1);
                    int num=map.get(arr[i]);
                    map.put(arr[i],num-1);
                    update(0,n-1,num,-1,0);
                }
                else
                {
                    cou.put(arr[i],cou.get(arr[i])+1);
                    int num=map.get(arr[i]);
                    map.put(arr[i],num-1);
                    update(0,n-1,num,-1,0);
                    count+=get(0,n-1,0,cou.get(arr[i])-1,0);
                    //System.out.println(p+" "+i+" "+cou.get(arr[i])+" "+map);
                }
                /*for(int j=0;j<=10;j++)
                {
                    System.out.println(tree[j]+" "+j);
                }*/
            }
            /*for(int i=0;i<(n<<2);i++)
            {
                System.out.println(tree[i]+" "+i);
            }*/
            System.out.println(count);
        }
        
    }
}
//import java.io.*;
class Reader {
		final private int BUFFER_SIZE = 1 << 16;
		private DataInputStream din;
		private byte [] buffer;
		private int bufferPointer, bytesRead;
		
		public Reader () {
			din = new DataInputStream (System.in);
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}
		
		public Reader (String file_name) throws IOException {
			din = new DataInputStream (new FileInputStream (file_name));
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}
		
		public String readLine () throws IOException {
			byte [] buf = new byte[1024];
			int cnt = 0, c;
			while ((c = read ()) != -1) {
				if (c == '\n')
					break;
				buf[cnt++] = (byte) c;
			}
			return new String (buf, 0, cnt);
		}
		
		public int nextInt () throws IOException {
			int ret = 0;
			byte c = read ();
			while (c <= ' ')
				c = read ();
			boolean neg = (c == '-');
			if (neg)
				c = read ();
			do {
				ret = ret * 10 + c - '0';
			} while ((c = read ()) >= '0' && c <= '9');
			if (neg)
				return -ret;
			return ret;
		}
		
		public long nextLong () throws IOException {
			long ret = 0;
			byte c = read ();
			while (c <= ' ')
				c = read ();
			boolean neg = (c == '-');
			if (neg)
				c = read ();
			do {
				ret = ret * 10 + c - '0';
			} while ((c = read ()) >= '0' && c <= '9');
			if (neg)
				return -ret;
			return ret;
		}
		
		public double nextDouble () throws IOException {
			double ret = 0, div = 1;
			byte c = read ();
			while (c <= ' ')
				c = read ();
			boolean neg = (c == '-');
			if (neg)
				c = read ();
			do {
				ret = ret * 10 + c - '0';
			} while ((c = read ()) >= '0' && c <= '9');
			if (c == '.')
				while ((c = read ()) >= '0' && c <= '9')
					ret += (c - '0') / (div *= 10);
			if (neg)
				return -ret;
			return ret;
		}
		
		private void fillBuffer () throws IOException {
			bytesRead = din.read (buffer, bufferPointer = 0, BUFFER_SIZE);
			if (bytesRead == -1)
				buffer[0] = -1;
		}
		
		private byte read () throws IOException {
			if (bufferPointer == bytesRead)
				fillBuffer ();
			return buffer[bufferPointer++];
		}
		
		public void close () throws IOException {
			if (din == null)
				return;
			din.close ();
		}
	}
