排队
在这里插入图片描述
数学

#pragma GCC optimize(2)
#include
#define endl "\n"
#define x first
#define y second
#define pb(a) push_back(a);
#define mst(a, x) memset(a, x, sizeof a)
#define all(a) a.begin(), a.end()
#define complete_unique(a) a.erase(unique(a.begin(), a.end()), a.end())
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int N = 1e5 + 10;
int cnt[N], n;
LL fact[N] = {0}, IV2 = (MOD + 1) / 2;
LL cn2(LL x)
{
return x * (x - 1) % MOD * IV2 % MOD;
}
void solve() {
int a;
cin >> n;
fact[1] = 1;
for(int i = 2; i < N; i ++) fact[i] = fact[i - 1] * i % MOD;
for(int i = 1; i <= n; i ++)
{
cin >> a;
cnt[a] ++;
}
LL res = cn2(n);
for(int i = 1; i <= (int)1e5; i ++)
{
res = ((res - cn2(cnt[i]) % MOD) + MOD) % MOD;
}
cout << fact[n] * res % MOD *IV2 % MOD << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) solve();
return 0;
}