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;
const long long N = 1001000;
long long a[N], b[N], c[N], sum[N];
struct Node {
  long long l, r, num;
};
Node tree[4 * N];
map<int, int> m1, m2;
void build(long long l, long long r, long long o) {
  tree[o].l = l, tree[o].r = r;
  tree[o].num = 0;
  if (l == r) return;
  long long mid = (l + r) >> 1;
  build(l, mid, o << 1);
  build(mid + 1, r, o + o + 1);
}
void update(long long t, long long o) {
  if (tree[o].l == tree[o].r && tree[o].l == t) {
    tree[o].num++;
    return;
  }
  long long mid = (tree[o].l + tree[o].r) >> 1;
  if (t > mid)
    update(t, o + o + 1);
  else
    update(t, o + o);
  tree[o].num = tree[o + o].num + tree[o + o + 1].num;
}
long long query(long long l, long long r, long long o) {
  if (tree[o].l == l && tree[o].r == r) {
    return tree[o].num;
  }
  long long mid = (tree[o].l + tree[o].r) >> 1;
  if (r <= mid)
    return query(l, r, o + o);
  else if (l > mid)
    return query(l, r, o + o + 1);
  else
    return query(l, mid, o * 2) + query(mid + 1, r, o * 2 + 1);
}
int main() {
  long long n;
  while (~scanf("%lld", &n)) {
    for (long long i = 0; i < n; i++) scanf("%lld", &a[i]);
    long long cnt = 1;
    for (long long i = 0; i < n; i++) {
      if (!m1[a[i]]) m1[a[i]] = cnt++;
      a[i] = m1[a[i]];
    }
    cnt = 0;
    memset(sum, 0, sizeof(sum));
    for (long long i = 0; i < n; i++) {
      sum[a[i]]++;
      b[i] = sum[a[i]];
      cnt = max(cnt, b[i]);
    }
    memset(sum, 0, sizeof(sum));
    for (long long i = n - 1; i >= 0; i--) {
      sum[a[i]]++;
      c[i] = sum[a[i]];
    }
    build(1, cnt, 1);
    long long ans = 0;
    for (long long i = 0; i < n; i++) {
      if (c[i] < cnt) ans += query(c[i] + 1, cnt, 1);
      update(b[i], 1);
    }
    printf("%lld\n", ans);
    m1.clear();
    m2.clear();
  }
  return 0;
}
