#include <bits/stdc++.h>
using namespace std;
long long a[1000006], n, f[1000006];
pair<int, int> b[1000006], x[1000006];
map<int, int> m;
void ud(long long x) {
  for (long long i = x; i <= n; i += i & (-i)) f[i] += 1;
}
long long Get(long long p) {
  long long sum = 0;
  for (long long i = p; i; i -= i & -i) sum += f[i];
  return sum;
}
int main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    m[a[i]]++;
    x[i] = make_pair(m[a[i]], i);
  }
  for (long long i = n; i >= 1; i--) {
    b[i] = make_pair(m[a[i]] - x[i].first + 1, i);
  }
  sort(x + 1, x + 1 + n);
  sort(b + 1, b + 1 + n);
  long long t = 1, tong = 0;
  b[n + 1] = make_pair(n + 1, n + 1);
  for (long long i = 1; i <= n + 1; i++) {
    while (t <= n && b[i].first >= x[t].first) {
      tong += Get(n) - Get(x[t].second);
      t++;
    }
    ud(b[i].second);
  }
  cout << tong;
  return 0;
}
