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
#include <bits/stdc++.h>
using namespace std;
int n, a[1005000], l[1005000], r[1005000];
long long ans, s[4 * 1005000];
map<int, int> ml, mr;
void update(int id, int l, int r, int pos, int val) {
  if (l > r) return;
  if (l == r) {
    s[id] += val;
    return;
  }
  int mid = (l + r) / 2;
  if (pos <= mid)
    update(2 * id, l, mid, pos, val);
  else
    update(2 * id + 1, mid + 1, r, pos, val);
  s[id] = s[2 * id] + s[2 * id + 1];
}
long long get(int id, int l, int r, int L, int R) {
  if (l > R || r < L) return 0;
  if (L <= l && r <= R) return s[id];
  int mid = (l + r) / 2;
  return get(2 * id, l, mid, L, R) + get(2 * id + 1, mid + 1, r, L, R);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) l[i] = ++ml[a[i]];
  for (int i = n; i > 0; i--) {
    r[i] = ++mr[a[i]];
    update(1, 1, n, r[i], 1);
  }
  for (int i = 1; i < n; i++) {
    update(1, 1, n, r[i], -1);
    ans += get(1, 1, n, 1, l[i] - 1);
  }
  cout << ans;
  return 0;
}
