#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma pack(1)
using namespace std;
using namespace __gnu_pbds;
tree<
pair<int,int>,
null_type,
less<pair<int,int>>,
rb_tree_tag,
tree_order_statistics_node_update> me;
#define TASK "test"
int main()
{
#ifndef ONLINE_JUDGE
freopen(TASK".in","r",stdin);
freopen(TASK".out","w",stdout);
#endif // ONLINE_JUDGE
ios::sync_with_stdio(0);
cin.tie(0);
map<int,int> cnt[2];
int n;
cin>>n;
vector<int> a(n);
for(auto &it:a) cin>>it;
vector<int> pre(n),suf(n);
for(int i=0;i<n;i++)
{
pre[i]=cnt[0][a[i]]++;
suf[n-i-1]=cnt[1][a[n-i-1]]++;
}
long long ans=0;
for(int i=1;i<n;i++)
{
me.insert({pre[i-1],i-1});
ans+=i-me.order_of_key({suf[i],i});
}
cout<<ans<<endl;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4KI2luY2x1ZGUgPGV4dC9wYl9kcy90cmVlX3BvbGljeS5ocHA+CiNwcmFnbWEgcGFjaygxKQoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdXNpbmcgbmFtZXNwYWNlIF9fZ251X3BiZHM7Cgp0cmVlPApwYWlyPGludCxpbnQ+LApudWxsX3R5cGUsCmxlc3M8cGFpcjxpbnQsaW50Pj4sCnJiX3RyZWVfdGFnLAp0cmVlX29yZGVyX3N0YXRpc3RpY3Nfbm9kZV91cGRhdGU+IG1lOwoKI2RlZmluZSBUQVNLICJ0ZXN0IgoKaW50IG1haW4oKQogewogICAgI2lmbmRlZiBPTkxJTkVfSlVER0UKICAgIGZyZW9wZW4oVEFTSyIuaW4iLCJyIixzdGRpbik7CiAgICBmcmVvcGVuKFRBU0siLm91dCIsInciLHN0ZG91dCk7CiAgICAjZW5kaWYgLy8gT05MSU5FX0pVREdFCiAgICBpb3M6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