#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 100005
ll a[maxn];
ll i, j, k, n, m, res, sum;
map<ll, vector<ll>> mapping;
ll solve(vector<ll> u)
{
sort(u.begin(), u.end());
ll pre_sum[u.size()];
memset(pre_sum, 0, sizeof(pre_sum));
pre_sum[0] = u[0];
for (ll i = 1; i < u.size(); i++)
pre_sum[i] = pre_sum[i - 1] + u[i];
ll tong = 0;
for (ll n = u.size() - 1; n >= 1; n--)
{
tong = tong + n * u[n] - pre_sum[n - 1];
}
return tong;
}
int main()
{
cin >> n;
for (i = 0; i < n; i++)
{
ll x;
cin >> x;
mapping[x].push_back(i);
}
for (auto p : mapping)
{
if (p.second.size() >= 2)
{
res = res + solve(p.second);
}
}
cout << res;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgbWF4biAxMDAwMDUKbGwgYVttYXhuXTsKbGwgaSwgaiwgaywgbiwgbSwgcmVzLCBzdW07Cm1hcDxsbCwgdmVjdG9yPGxsPj4gbWFwcGluZzsKbGwgc29sdmUodmVjdG9yPGxsPiB1KQp7CiAgICBzb3J0KHUuYmVnaW4oKSwgdS5lbmQoKSk7CiAgICBsbCBwcmVfc3VtW3Uuc2l6ZSgpXTsKICAgIG1lbXNldChwcmVfc3VtLCAwLCBzaXplb2YocHJlX3N1bSkpOwogICAgcHJlX3N1bVswXSA9IHVbMF07CiAgICBmb3IgKGxsIGkgPSAxOyBpIDwgdS5zaXplKCk7IGkrKykKICAgICAgICBwcmVfc3VtW2ldID0gcHJlX3N1bVtpIC0gMV0gKyB1W2ldOwogICAgbGwgdG9uZyA9IDA7CiAgICBmb3IgKGxsIG4gPSB1LnNpemUoKSAtIDE7IG4gPj0gMTsgbi0tKQogICAgewogICAgICAgIHRvbmcgPSB0b25nICsgbiAqIHVbbl0gLSBwcmVfc3VtW24gLSAxXTsKICAg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