// // Created by yamunaku on 2020/08/22. // #include using namespace std; #define rep(i, n) for(ll i = 0; i < (n); i++) #define repl(i, l, r) for(ll i = (l); i < (r); i++) #define per(i, n) for(int i = ((n)-1); i >= 0; i--) #define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--) #define all(x) (x).begin(),(x).end() #define MOD 998244353 #define MOD1 1000000007 #define IINF 1000000000 #define LINF 1000000000000000000 #define SP <<" "<< #define CYES cout<<"Yes"<; using mti = vector>; using vl = vector; using mtl = vector>; using pi = pair; using pl = pair; template using heap = priority_queue, function>; ll modpow(ll x, ll a){ ll ans = 1; while(a){ if(a & 1) ans = ans * x % MOD; a >>= 1; x = x * x % MOD; } return ans; } ll inv(ll x){ return modpow(x, MOD - 2); } struct SegmentTree{ int ide = 0; inline void updatef(int &x, int y){ x += y; } inline int queryf(int x, int y){ return x + y; } int n; vector node; SegmentTree(int sz){ n = 1; while(n < sz) n <<= 1; node.resize(2 * n - 1, ide); } void update(int k, int x){ k += n - 1; updatef(node[k], x); while(k){ k = (k - 1) / 2; node[k] = queryf(node[2 * k + 1], node[2 * k + 2]); } } int get(int a, int b, int k = 0, int l = 0, int r = -1){ if(r < 0)r = n; if(r <= a || b <= l) return ide; if(a <= l && r <= b) return node[k]; int xl = get(a, b, 2 * k + 1, l, (l + r) / 2); int xr = get(a, b, 2 * k + 2, (l + r) / 2, r); return queryf(xl, xr); } }; int main(){ //CFS; int n; cin >> n; if(n == 1){ cout << 0 << endl; } vi a(n); map c; rep(i, n){ cin >> a[i]; c[a[i]]++; } vi idx(n); iota(all(idx), 0); sort(all(idx), [&](int l, int r){ return a[l] < a[r]; }); ll ans1 = 0; SegmentTree seg(n); per(i, n){ ans1 += seg.get(0, idx[i]); seg.update(idx[i], 1); } ll ans2 = 0; ll sum = 0; for(auto &p : c){ ans2 += sum * p.second; sum += p.second; } ll k = 1; ll cm = 1; repl(i, 1, n + 1){ cm = cm * (n - i + 1) % MOD * inv(i) % MOD; k = k * cm % MOD; } ll sum1 = 0; repl(i, 2, n + 1){ sum1 = (sum1 + i * (i - 1)) % MOD; } sum1 = sum1 * k % MOD * inv(n * (n - 1) % MOD) % MOD; ll sum2 = 0; rep(i, n){ sum2 = (sum2 + (i + 1) * (i + 1) * i / 2) % MOD; } sum2 = sum2 * k % MOD * inv(n * n % MOD) % MOD; // cout << ans1 SP sum1 SP ans2 SP sum2 << endl; cout << (ans1 * sum1 % MOD + ans2 * sum2 % MOD) % MOD << endl; return 0; }