#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100;
int seg[4 * maxn], dp[maxn], cnt[maxn], n, a[maxn], lef[maxn], rig[maxn];
set<pair<int, int> > s;
long long get(int id, int L, int R, int l, int r) {
  if (L == l and R == r) return seg[id];
  int mid = (L + R) / 2;
  long long ret = 0;
  if (mid > l) ret += get(2 * id + 0, L, mid, l, min(mid, r));
  if (mid < r) ret += get(2 * id + 1, mid, R, max(mid, l), r);
  return ret;
}
void change(int id, int L, int R, int idx) {
  if (L + 1 == R) {
    seg[id]++;
    return;
  }
  seg[id]++;
  int mid = (L + R) / 2;
  if (mid > idx)
    change(2 * id + 0, L, mid, idx);
  else
    change(2 * id + 1, mid, R, idx);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    s.insert(make_pair(a[i], i));
  }
  int mex = 0, pre = -1;
  while (!s.empty()) {
    int u = (*s.begin()).second;
    if (a[u] != pre) {
      pre = a[u];
      mex++;
    }
    a[u] = mex;
    s.erase(s.begin());
  }
  for (int i = 1; i <= n; i++) lef[i] = ++cnt[a[i]];
  memset(cnt, 0, sizeof cnt);
  for (int i = n; i >= 1; i--) rig[i] = ++cnt[a[i]];
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (rig[i] != n) ans += get(1, 1, n + 1, rig[i] + 1, n + 1);
    change(1, 1, n + 1, lef[i]);
  }
  cout << ans << endl;
}
