#include #include #include #include using namespace std; #define PB push_back typedef long long int ll; constexpr int kN = int(2E5 + 10), kMod = 998244353; struct BIT { int val[kN]; void init() {memset(val, 0, sizeof(val));} void add(int pos) { while (pos < kN) { val[pos]++; pos += pos & -pos; } return ; } int ask(int pos) { int ans = 0; while (pos) { ans += val[pos]; pos ^= pos & -pos; } return ans; } }; ll Pow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans = ans * a % kMod; a = a * a % kMod; b >>= 1; } return ans; } ll Rev(ll n) {return Pow(n, kMod - 2);} ll f[kN], inf[kN]; void pre() { f[0] = f[1] = inf[0] = inf[1] = 1; for (int i = 2; i < kN; i++) f[i] = f[i - 1] * i % kMod; inf[kN - 1] = Rev(f[kN - 1]); for (int i = kN - 1; i > 2; i--) inf[i - 1] = inf[i] * i % kMod; return ; } ll C(int n, int m) {return f[n] * inf[m] % kMod * inf[n - m] % kMod;} int a[kN], rnk[kN]; ll same[kN]; BIT bit; int main() { int n; ll ans = 0, tot = 1, tmp = 0, cnt = 0, ccnt = 0; vector v; scanf("%d", &n); if (n == 1) { printf("0\n"); return 0; } for (int i = 1; i <= n; i++) scanf("%d", &a[i]); pre(); for (int i = 1; i <= n; i++) tot = tot * C(n, i) % kMod; // inside bit.init(); for (int i = 1; i <= n; i++) v.PB(a[i]); sort(v.begin(), v.end()); v.resize(unique(v.begin(), v.end()) - v.begin()); for (int i = 1; i <= n; i++) rnk[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1; for (int i = 1; i <= n; i++) { cnt += (i - 1) - bit.ask(rnk[i]); bit.add(rnk[i]); } for (int i = 2; i <= n; i++) tmp += ll(i) * (i - 1) % kMod; tmp %= kMod; ans = tmp * tot % kMod * Rev(n) % kMod * Rev(n - 1) % kMod * cnt % kMod; // between for (int i = 1; i <= n; i++) same[rnk[i]]++; for (int i = 1; i <= n; i++) if (same[rnk[i]]) { ccnt += (n - same[rnk[i]]) * same[rnk[i]] % kMod; same[rnk[i]] = 0; } ccnt %= kMod; ccnt = ccnt * Rev(2) % kMod; tmp = ll(n) * (n + 1) % kMod * Rev(2) % kMod; tmp = tmp * tmp % kMod; for (int i = 1; i <= n; i++) tmp -= ll(i) * i % kMod; tmp %= kMod; if (tmp < 0) tmp += kMod; tmp = tmp * Rev(2) % kMod; ans += tmp * tot % kMod * ccnt % kMod * Rev(n) % kMod * Rev(n) % kMod; printf("%lld\n", ans % kMod); }