#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; using ull = unsigned long long; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); } return a; } //mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); constexpr long long INF = 1LL << 60; constexpr int inf = 1000000007; //constexpr long long mod = 1000000007LL; constexpr long long mod = 998244353; constexpr int MAX = 1100000; template struct Compress { vector v; Compress() {} Compress(vector _v) :v(_v) { build(); } void add(T x) { v.emplace_back(x); } void build() { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } void build(vector _v) { v = _v; sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } int get(T x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } T& operator[](int i) { return v[i]; } int size() { return (int)v.size(); } }; template struct BIT { int n; vector bit; BIT() :n(0) {} BIT(int _n) :n(_n) { bit = vector(n + 1); } void add1(int idx, T val) { for (int i = idx; i <= n; i += i & -i) bit[i] += val; } T sum1(int idx) { T res = 0; for (int i = idx; i > 0; i -= i & -i) res += bit[i]; return res; } //0-indexed void add(int idx, T val) { add1(idx + 1, val); } //0-indexed [left, right) T sum(int left, int right) { return sum1(right) - sum1(left); } int lower_bound(T x) { int res = 0; int k = 1; while (2 * k <= n) k <<= 1; for (; k > 0; k >>= 1) { if (res + k <= n and bit[res + k] < x) { x -= bit[res + k]; res += k; } } return res; } }; struct mint { long long x; mint(long long x = 0) :x((x% mod + mod) % mod) {} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint& operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; 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; } } mint COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return mint(fac[n] * (finv[k] * finv[n - k] % mod) % mod); } mint LCOM(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; if (k > n / 2) k = n - k; mint res = 1; for (int i = 0; i < k; i++) res *= (n - i); res *= finv[k]; return res; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); COMinit(); int n; cin >> n; vector a(n); for (int i = 0; i < n; i++) cin >> a[i]; Compress comp(a); for (int i = 0; i < n; i++) { a[i] = comp.get(a[i]); } mint res = 0; mint c1 = 0, c2 = 0; { BIT bit(n); for (int i = 0; i < n; i++) { c1 += bit.sum(0, a[i]); c2 += bit.sum(a[i] + 1, n); bit.add(a[i], 1); } } for (int i = 2; i <= n; i++) { res += COM(n - 2, i - 2) / COM(n, i) * c2; } mint ninv = mint(n).inv(); for (int i = 2; i <= n; i++) { mint p = mint(i) * ninv * mint(1LL * i * (i - 1) / 2) * ninv; res += p * (c1 + c2); } for (int i = 1; i <= n; i++) res *= COM(n, i); cout << res.x << "\n"; }