#include <bits/stdc++.h>
using namespace std;
const int N = 1000000 + 5;
int a[N], b[N], c[N], d[N], n, h[N], g[N], tr[N];
int al[N], ar[N];
struct code {
  int s, t;
};
code nu[N], nx1[N], nx2[N];
bool cmp(code x, code y) {
  if (x.s == y.s) return x.t < y.t;
  return x.s < y.s;
}
int lowbit(int x) { return x & (-x); }
int sum(int x) {
  int ret = 0;
  while (x > 0) {
    ret += tr[x];
    x -= lowbit(x);
  }
  return ret;
}
void update(int x) {
  while (x <= n) {
    tr[x] += 1;
    x += lowbit(x);
  }
}
void work() {
  sort(nu + 1, nu + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    b[i] = nu[i].s;
    c[i] = nu[i].t;
  }
  for (int i = 1; i <= n; i++) {
    int lt = lower_bound(b + 1, b + n + 1, a[i]) - b;
    int rt = upper_bound(b + 1, b + n + 1, a[i]) - b;
    int an = lower_bound(c + lt, c + rt, i) - c;
    al[i] = an - lt + 1;
    ar[i] = rt - an;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    nx1[i].s = al[i];
    nx2[i].s = ar[i];
    nx1[i].t = i;
    nx2[i].t = i;
  }
  sort(nx1 + 1, nx1 + n + 1, cmp);
  sort(nx2 + 1, nx2 + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    g[i] = nx1[i].s;
    h[i] = nx2[i].s;
  }
  int gq = unique(g + 1, g + n + 1) - (g + 1);
  int hq = unique(h + 1, h + n + 1) - (h + 1);
  for (int i = 1; i <= n; i++) {
    int gp = lower_bound(g + 1, g + gq + 1, al[i]) - g;
    int hp = lower_bound(h + 1, h + hq + 1, ar[i]) - h;
    if (hp <= gq)
      ans += sum(gq) - sum(hp);
    else
      ans += 0;
    update(gp);
  }
  printf("%I64d\n", ans);
}
int main() {
  while (scanf("%d", &n) == 1) {
    memset(tr, 0, sizeof(tr));
    for (int i = 1; i <= n; i++) {
      scanf("%d", a + i);
      nu[i].s = a[i];
      nu[i].t = i;
    }
    work();
  }
  return 0;
}
