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 math,sys,bisect,heapq,os
from collections import defaultdict,Counter,deque
from itertools import groupby,accumulate
from functools import lru_cache
#sys.setrecursionlimit(200000000)
pr = lambda x:    x
def input(): return sys.stdin.readline().rstrip('\r\n')
#input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
aj = lambda: list(map(int, input().split()))
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
#MOD = 1000000000 + 7
def Y(c):  print(["NO","YES"][c])
def y(c):  print(["no","yes"][c])
def Yy(c):  print(["No","Yes"][c])


def solve():
    n, = aj()
    B = aj()
    if n <= 2:
        print(0)
    elif len(set(B)) == n:
        print(0)
    else:
        def BIT(n):
            Tree = [0]*(n+1)
            arr = [0]*(n+1)
            return Tree,arr

        def update(i,dif):
            arr[i] += dif
            i+=1
            while i<=n:
                Tree[i] += dif
                i += i&(-i)
                
        def getvalue(i):
            s= 0
            i += 1 
            while i>0:
                s += Tree[i]
                i -= i&(-i)
            return s

        def query(l,r):
            if l == 0:
                return getvalue(r)
            else:
                return getvalue(r) - getvalue(l-1)

        ans= 0;C = Counter()
        A = [0]*n
        for i in range(n-1,-1,-1):
            A[i] = C[B[i]] + 1
            C[B[i]] += 1
        Tree,arr = BIT(n+1)
        for i in range(1,n):
            update(A[i],1)
        # pr(arr)
        # pr(A)
        C= Counter()
        C[B[0]] += 1
        for i in range(1,n-1):
            C[B[i]] += 1
            v = C[B[i]]
            update(A[i],-1)
            count = query(0,v-1)
            ans += count
        print(ans)



try:
    #os.system("online_judge.py")
    sys.stdin = open('input.txt', 'r') 
    sys.stdout = open('output.txt', 'w')
    from aj import *
except:
    pass

solve()