#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; template struct BinaryIndexedTree { int n; vector bit; BinaryIndexedTree() :n(0) {} BinaryIndexedTree(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; } }; 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(); } }; 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; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector a(n); for (int i = 0; i < n; i++) cin >> a[i]; Compress comp(a); vector lsum(n), lcnt(n), rsum(n), rcnt(n); { BinaryIndexedTree bsum(n), bcnt(n); for (int i = 0; i < n; i++) { int t = comp.get(a[i]); lcnt[i] = bcnt.sum(t + 1, n); lsum[i] = bsum.sum(t + 1, n); bcnt.add(t, 1); bsum.add(t, a[i]); } } { BinaryIndexedTree bsum(n), bcnt(n); for (int i = n - 1; i >= 0; i--) { int t = comp.get(a[i]); rcnt[i] = bcnt.sum(0, t); rsum[i] = bsum.sum(0, t); bcnt.add(t, 1); bsum.add(t, a[i]); } } mint res = 0; for (int i = 0; i < n; i++) { lsum[i] %= mod; lcnt[i] %= mod; rcnt[i] %= mod; rsum[i] %= mod; res += lsum[i] * rcnt[i]; res += lcnt[i] * rsum[i]; res += mint(a[i]) * lcnt[i] * rcnt[i]; } cout << res.x << "\n"; }