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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  sem título.py
#  
#  Copyright 2020 Alencar <Alencar@ALENCAR-PC>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  



n=int(input())
def getsum(BITTree,i): 
    s = 0
    i = i+1
    while i > 0: 
        s += BITTree[i] 
        i -= i & (-i) 
    return s  
def updatebit(BITTree , n , i ,v): 
    i += 1
    while i <= n: 
        BITTree[i] += v 
        i += i & (-i) 
a=list(map(int,input().split()))
pre=dict()
pos=dict()
for i in range(n):
    if(pre.get(a[i],None)==None):
        pre[a[i]]=0
    pre[a[i]]+=1 
ans=0
BIT=[0]*(n+1)
for i in range(n-1,0,-1):
    pre[a[i]]-=1 
    if(pos.get(a[i],None)==None):
        pos[a[i]]=0
    pos[a[i]]+=1 
    updatebit(BIT,n,pos[a[i]],1)
    temp=getsum(BIT,pre[a[i-1]]-1)
    ans+=temp
print(ans)

	 					   		 			   						   			