#include #define rep(i,n) for(ll i = 0; i < (n); ++i) #define drep(i,n) for(ll i = (n)-1; i >= 0; --i) #define srep(i,s,t) for (ll i = s; i < t; ++i) using namespace std; typedef long long int ll; typedef pair P; #define yn {puts("Yes");}else{puts("No");} ll mergecount(vector &a) { ll count = 0; int n = a.size(); if (n > 1) { vector b(a.begin(), a.begin() + n/2); vector c(a.begin() + n/2, a.end()); count += mergecount(b); count += mergecount(c); for (int i = 0, j = 0, k = 0; i < n; ++i) if (k == c.size()) a[i] = b[j++]; else if (j == b.size()) a[i] = c[k++]; else if (b[j] <= c[k]) a[i] = b[j++]; else { a[i] = c[k++]; count += n/2 - j; } } return count; } const int MAX = 510000; const int MOD = 998244353; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++){ fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k){ if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } long long FINV(int n){ if (n < 0) return 0; return finv[n]; } // ax + by = gcd(a, b) となるような (x, y) を求める // a と b は互いに素として ax + by = 1 となる (x, y) を求める long long extGCD(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = extGCD(b, a%b, y, x); // 再帰 y -= a / b * x; return d; } // 負の数に対応した mod inline long long mod(long long a, long long m) { return (a % m + m) % m; } // 逆元計算 (a と m が互いに素であることが必要) long long modinv(long long a, long long m) { long long x, y; extGCD(a, m, x, y); return mod(x, m); // x % m だが、x が負かもしれないので } int main() { COMinit(); ll n; cin >> n; vector a(n); rep(i,n) cin >> a[i]; ll ten = mergecount(a); ten %= MOD; map mp; rep(i,n)mp[a[i]]++; ll diff = 0; rep(i,n){ diff += (n-mp[a[i]]); } diff /= 2; // cout << "diff = " << diff << endl; ll mul = 1; srep(i,1,n+1){ mul *= COM(n,i); mul %= MOD; } ll all = mul; ll sum2 = 0; srep(i,1,n+1){ sum2 += (ll)i * (((n*(n+1)/2) - i*(i+1)/2)%MOD) % MOD; sum2 %= MOD; } all = all * sum2 % MOD; // all *= (n*(n+1)%MOD*((n*(n+1)-2)%MOD)%MOD*modinv(8,MOD)%MOD)%MOD; // cout << all << endl; ll mine = mul * modinv(n*n,MOD) % MOD; ll sum = 0; srep(i,1,n+1){ ll tmp = n*(n+1)/2 - i*(i+1)/2; sum += tmp; sum %= MOD; } mine = mine * sum % MOD * n % MOD; // cout << mine << endl; // ll ans1 = (all + MOD - mine) % MOD * diff % MOD * modinv(n*(n-1),MOD) % MOD; ll ans1 = all % MOD * diff % MOD * modinv(n*n,MOD) % MOD; ll sum3 = 0; ll mine2 = mul * modinv(n*(n-1),MOD) % MOD; srep(i,2,n+1){ sum3 += i * (i - 1); sum3 %= MOD; } mine2 = mine2 * sum3 % MOD; ll ans2 = mine2 * ten % MOD; ll ans = (ans1 + ans2) % MOD; // cout << ans1 << ' ' << ans2 << endl; cout << ans << endl; return 0; }