#include #include #include #include #include #include #include #include #include using namespace std; #define int long long #define endl "\n" constexpr long long INF = (long long)1e18; constexpr long long MOD = 998244353; struct fast_io { fast_io(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); }; } fio; template class segment_tree{ int n; T fval; function operation; vector dat; T _query(int a, int b, int k, int l, int r){ if(r <= a || b <= l){ return fval; } if(a <= l && r <= b){ return dat[k]; }else { T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2 ); T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r); return operation(vl, vr); } } public: segment_tree(){ } segment_tree(int n_, T val, function fun){ init(n_, val, fun); } ~segment_tree(){ } void init(int n_, T val, function fun){ fval = val; operation = fun; n = 1; while(n < n_) n *= 2; dat.resize(2 * n - 1); for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval; } void update(int k, T a){ k += n - 1; dat[k] += a; while(k > 0){ k = (k - 1) / 2; dat[k] = operation(dat[k*2 + 1],dat[k*2 + 2]); } } T query(int a, int b){ return _query(a, b, 0, 0, n); } }; signed main(){ cout< A, B, C; segment_tree segl, segr; segment_tree segl2, segr2; cin>>N; A.resize(N); B.resize(N); C.resize(N); segl.init(N+1,0, [](int first, int second){ return (first+second);}); segr.init(N+1,0, [](int first, int second){ return (first+second);}); segl2.init(N+1,0, [](int first, int second){ return (first+second);}); segr2.init(N+1,0, [](int first, int second){ return (first+second);}); for(int i = 0; i < N; i++){ cin>>A[i]; } B = A; C = A; sort(B.begin(), B.end()); for(int i = 0; i < N; i++){ C[i] = lower_bound(B.begin(), B.end(),A[i]) - B.begin(); segr.update(C[i], 1); segr2.update(C[i], A[i]); } for(int i = 0; i < N; i++){ segr.update(C[i], -1); segr2.update(C[i], -A[i]); ans += segl.query(C[i]+1,N)%MOD * (segr.query(0, C[i])%MOD) % MOD * A[i] % MOD; ans += segl.query(C[i]+1,N)%MOD * (segr2.query(0, C[i])%MOD) % MOD; ans += segl2.query(C[i]+1,N)%MOD * (segr.query(0, C[i])%MOD) % MOD; ans %= MOD; // cout<