#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000005;   class fenwick {     int n;     vector<int> v;       public:     fenwick(int _n) : n(_n) { v.resize(n); }     void update(int at, int by) {       while (at < v.size()) {         v[at] += by;         at |= (at + 1);       }     }     int query(int at) {       int res = 0;       while (at >= 0) {         res += v[at];         at = (at & (at + 1)) - 1;       }       return res;     }   };   map<int, int> indexes;   int get_index(int x) {     if (indexes.count(x) == 0) {       int sz = indexes.size();       indexes[x] = sz;     }     return indexes[x];   }   vector<int> f(vector<int> &v) {     int n = v.size();     vector<int> cnt(maxn, 0);     vector<int> ret;     for (auto &x : v) {       cnt[x]++;       ret.push_back(cnt[x]);     }     return ret;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     int n;     cin >> n;     vector<int> v(n);     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       v[i] = get_index(x);     }     auto left = f(v);     reverse(v.begin(), v.end());     auto right = f(v);     reverse(right.begin(), right.end());     fenwick ft = fenwick(maxn);     for (int i = 0; i < n; ++i) {       ft.update(right[i], 1);     }     long long ans = 0;     for (int i = 0; i < n; ++i) {       ft.update(right[i], -1);       int cnt = ft.query(left[i] - 1);       ans += cnt;     }     cout << ans <<   n ;     return 0;   }   