#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;
}