459_D. Pashmak and Parmida's problem
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
```python
# 459D
import sys
from collections import Counter

class BIT():
    def __init__(self, n):
        self.n = n
        self.tree = [0] * n
    
    def _F(self, i):
        return i & (i + 1)
    
    def _getSum(self, r):
        '''
        sum on interval [0, r]
        '''
        result = 0
        while r >= 0:
            result += self.tree[r]
            r = self._F(r) - 1
        return result
    
    def getSum(self, l, r):
        '''
        sum on interval [l, r]
        '''
        return self._getSum(r) - self._getSum(l - 1)
    
    def _H(self, i):
        return i | (i + 1)
    
    def add(self, i, value=1):
        while i < self.n:
            self.tree[i] += value
            i = self._H(i)

# inf = open('input.txt', 'r')
# reader = (line.rstrip() for line in inf)
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__

n = int(input())
a = list(map(int, input().split()))
    
# inf.close()

freq = BIT(n+1)
f_left = [0] * n
f_right = [0] * n
ctr = Counter()
for i, val in enumerate(a):
    ctr[val] += 1
    f_left[i] = ctr[val]
for i in range(n):
    val = a[i]
    f_right[i] = ctr[val] - f_left[i] + 1
for f_r in f_right:
    freq.add(f_r, 1)
ans = 0
for i, f_l in enumerate(f_left):
    f_r = f_right[i]
    freq.add(f_r, -1)
    ans += freq.getSum(1, f_l-1)
print(ans)

```