#include using namespace std; #define ll long long #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) //勉強用写し const int MOD = 998244353; struct mint { long long x; // typedef long long ll; mint(long long x=0):x((x%MOD+MOD)%MOD){} mint operator-() const { return mint(-x);} 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 { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(long long 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 { return mint(*this) /= a;} }; istream& operator>>(istream& is, const mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} template struct BIT { int n; vector d; BIT(int n=0):n(n),d(n+1) {} void add(int i, T x=1) { for (i++; i <= n; i += i&-i) { d[i] += x; } } T sum(int i) { T x = 0; for (i++; i; i -= i&-i) { x += d[i]; } return x; } T sum(int l, int r) { return sum(r-1) - sum(l-1); } }; template std::vector press(std::vector &x) { auto res = x; std::sort(res.begin(), res.end()); res.erase(std::unique(res.begin(), res.end()), res.end()); for(int i = 0; i < (int)x.size(); i++) x[i] = std::lower_bound(res.begin(), res.end(), x[i]) - res.begin(); return res; } mint res; int main() { int n; cin >> n; vector a(n,0); rep(i,n) cin >> a[i]; //座標圧縮 auto rec = press(a); int m = rec.size(); //以下は前準備 vector x(n), y(n); vector l(n), r(n); { BIT BT(m); BIT kosuu(m); for(int i = 0; i < n; i++) { BT.add(a[i], rec[a[i]]); kosuu.add(a[i], 1); x[i] = BT.sum(a[i] + 1, m); l[i] = kosuu.sum(a[i] + 1, m); } } { BIT BT(m); BIT kosuu(m); for(int i = n - 1; i >= 0; i--) { BT.add(a[i], rec[a[i]]); kosuu.add(a[i], 1); y[i] = BT.sum(0, a[i]); r[i] = kosuu.sum(0, a[i]); } } //答えの計算 for(int j = 0; j < n; j++) { if(!l[j] * r[j]) continue; mint tmp = rec[a[j]]; res += x[j] * r[j] + y[j] * l[j] + tmp * l[j] * r[j]; } cout << res << endl; return 0; }