#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>order_set;
typedef pair<int, int>pr;
#define all(i) i.begin() , i.end()
#define ft first
#define sn second
#define pb push_back
#define en "\n"
#define MAXN 1000010
#define inf 1e6+5
const ll mod = 1e9 + 7;
ll n;
ll a[MAXN];
void solve()
{
cin >> n;
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
ll ans = 0;
ll x = 0;
for (ll i = 0; i < n; i++)
{
ll y = 1;
if (a[i] == a[i + 1] && i + 1 < n)
{
while (a[i] == a[i + 1] && i + 1 < n) {
y++;
i++;
}
}
ll z = n - (x + y);
ans += (((x * y) % mod) * z) % mod;
x += y;
ans %= mod;
}
cout << ans << en;
}
int main()
{
IOS;
ll t;
t = 1;
// cin >> t;
int c = 0;
while ( t-- )
{
// cout<<"Case "<<++c<<": ";
solve();
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnR5cGVkZWYgbG9uZyBsb25nIGludCBsbDsKI2RlZmluZSBJT1MgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsgIGNpbi50aWUoMCk7IGNvdXQudGllKDApOwoKCiNpbmNsdWRlIDxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4KI2luY2x1ZGUgPGV4dC9wYl9kcy90cmVlX3BvbGljeS5ocHA+CnVzaW5nIG5hbWVzcGFjZSBfX2dudV9wYmRzOwoKdHlwZWRlZiB0cmVlPGludCwgbnVsbF90eXBlLCBsZXNzPGludD4sIHJiX3RyZWVfdGFnLCB0cmVlX29yZGVyX3N0YXRpc3RpY3Nfbm9kZV91cGRhdGU+b3JkZXJfc2V0Owp0eXBlZGVmIHBhaXI8aW50LCBpbnQ+cHI7CiNkZWZpbmUgYWxsKGkpICAgICBpLmJlZ2luKCkgLCBpLmVuZCgpCiNkZWZpbmUgZnQgICAgIGZpcnN0CiNkZWZpbmUgc24gICAgIHNlY29uZAojZGVmaW5lIHBiIHB1c2hfYmFjawoKI2RlZmluZSBlbiAiXG4iCgojZGVmaW5lIE1BWE4gMTAwMDAxMAojZGVmaW5lIGluZiAxZTYrNQpjb25zdCBsbCBtb2QgPSAxZTkgKyA3OwoKbGwgbjsKbGwgYVtNQVhOXTsKCnZvaWQgc29sdmUoKQp7CgogICAgY2luID4+IG47CiAgICBmb3IgKGxsIGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgY2luID4+IGFbaV07CiAgICB9CgogICAgc29ydChhLCBhICsgbik7CgogICAgbGwgYW5zID0gMDsKCiAgICBsbCB4ID0gMDsKICAgIGZvciAobGwgaSA9IDA7IGkgPCBuOyBpKyspCiAgICB7CiAgICAgICAgbGwgeSA9IDE7CiAgICAgICAgaWYgKGFbaV0gPT0gYVtpICsgMV0gJiYgaSArIDEgPCBuKQogICAgICAgIHsKICAgICAgICAgICAgd2hpbGUgKGFbaV0gPT0gYVtpICsgMV0gJiYgaSArIDEgPCBuKSB7CiAgICAgICAgICAgICAgICB5Kys7CiAgICAgICAgICAgICAgICBpKys7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgbGwgeiA9IG4gLSAoeCArIHkpOwogICAgICAgIGFucyArPSAoKCh4ICogeSkgJSBtb2QpICogeikgJSBtb2Q7CiAgICAgICAgeCArPSB5OwogICAgICAgIGFucyAlPSBtb2Q7CiAgICB9CgogICAgY291dCA8PCBhbnMgPDwgZW47Cgp9CmludCBtYWluKCkKewogICAgSU9TOwogICAgbGwgdDsKICAgIHQgPSAxOwoKICAgIC8vIGNpbiA+PiB0OwoKCiAgICBpbnQgYyA9IDA7CiAgICB3aGlsZSAoIHQtLSApCiAgICB7CiAgICAgICAgLy8gY291dDw8IkNhc2UgIjw8KytjPDwiOiAiOwogICAgICAgIHNvbHZlKCk7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==