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 int N = 1e6 + 5;
int tree1[N], tree2[N], tree[N];
int n;
int a[N], l[N], r[N];
map<int, int> mp;
int query1(int x) {
  int ans = 0;
  while (x) {
    ans += tree1[x];
    x -= (x & -x);
  }
  return ans;
}
int query2(int x) {
  int ans = 0;
  while (x < N) {
    ans += tree2[x];
    x += (x & -x);
  }
  return ans;
}
void update1(int x) {
  while (x < N) {
    tree1[x]++;
    x += (x & -x);
  }
}
void update2(int x) {
  while (x) {
    tree2[x]++;
    x -= (x & -x);
  }
}
int query(int x) {
  int ans = 0;
  while (x) {
    ans += tree[x];
    x -= (x & -x);
  }
  return ans;
}
void update(int x) {
  while (x < N) {
    tree[x]++;
    x += (x & -x);
  }
}
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];
  set<int> s;
  for (int i = 1; i <= n; i++) s.insert(a[i]);
  int cnt = 0;
  for (int x : s) mp[x] = ++cnt;
  for (int i = 1; i <= n; i++) a[i] = mp[a[i]];
  memset(tree1, 0, sizeof tree1);
  memset(tree2, 0, sizeof tree2);
  for (int i = 1; i <= n; i++) {
    l[i] = i - query1(a[i] - 1) - query2(a[i] + 1);
    update1(a[i]);
    update2(a[i]);
  }
  memset(tree1, 0, sizeof tree1);
  memset(tree2, 0, sizeof tree2);
  for (int i = n; i >= 1; i--) {
    r[i] = n - i + 1 - query1(a[i] - 1) - query2(a[i] + 1);
    update1(a[i]);
    update2(a[i]);
  }
  memset(tree, 0, sizeof tree);
  int64_t ans = 0;
  for (int i = n; i >= 1; i--) {
    ans += query(l[i] - 1);
    update(r[i]);
  }
  cout << ans;
  return 0;
}
