class BIT: def __init__(self,sz): self.vals=[0 for i in range(sz+1)] self.size=sz def getVal(self,i): # answer vals[1-i] ret=0 while i!=0: ret+=self.vals[i] i-=i&(-i) return ret def addVal(self,i,v): # add v to i , note i is in [1,size] while i<=self.size: self.vals[i]+=v i+=i&(-i) N=int(raw_input()) A=map(int,raw_input().split()) S=list(set(A)) S.sort() unZip={v:i+1 for i,v in enumerate(S)} sameArray=[[] for i in range(len(S)+1)] for i,a in enumerate(A): sameArray[unZip[a]].append(i) ans = 0 bleft=BIT(len(S)) bright=BIT(len(S)) for a in A: bright.addVal(unZip[a],1) for a in A: ai=unZip[a] bright.addVal(ai,-1) ans += bleft.getVal(ai-1)*bright.getVal(ai-1) ans += (bleft.getVal(len(S))-bleft.getVal(ai))* (bright.getVal(len(S))-bright.getVal(ai)) bleft.addVal(ai,1) for s in range(len(S)+1): for i in range(len(sameArray[s])-1): nleft=i+1 nright=len(sameArray[s])-nleft ans -= (sameArray[s][i+1]-sameArray[s][i]-1)*nleft*nright print ans