#include #include #include #include namespace atcoder { namespace internal { #ifndef _MSC_VER template using is_signed_int128 = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using is_unsigned_int128 = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using make_unsigned_int128 = typename std::conditional::value, __uint128_t, unsigned __int128>; template using is_integral = typename std::conditional::value || is_signed_int128::value || is_unsigned_int128::value, std::true_type, std::false_type>::type; template using is_signed_int = typename std::conditional<(is_integral::value && std::is_signed::value) || is_signed_int128::value, std::true_type, std::false_type>::type; template using is_unsigned_int = typename std::conditional<(is_integral::value && std::is_unsigned::value) || is_unsigned_int128::value, std::true_type, std::false_type>::type; template using to_unsigned = typename std::conditional< is_signed_int128::value, make_unsigned_int128, typename std::conditional::value, std::make_unsigned, std::common_type>::type>::type; #else template using is_integral = typename std::is_integral; template using is_signed_int = typename std::conditional::value && std::is_signed::value, std::true_type, std::false_type>::type; template using is_unsigned_int = typename std::conditional::value && std::is_unsigned::value, std::true_type, std::false_type>::type; template using to_unsigned = typename std::conditional::value, std::make_unsigned, std::common_type>::type; #endif template using is_signed_int_t = std::enable_if_t::value>; template using is_unsigned_int_t = std::enable_if_t::value>; template using to_unsigned_t = typename to_unsigned::type; } // namespace internal } // namespace atcoder #include #include namespace atcoder { // Reference: https://en.wikipedia.org/wiki/Fenwick_tree template struct fenwick_tree { using U = internal::to_unsigned_t; public: fenwick_tree() : _n(0) {} fenwick_tree(int n) : _n(n), data(n) {} void add(int p, T x) { assert(0 <= p && p < _n); p++; while (p <= _n) { data[p - 1] += U(x); p += p & -p; } } T sum(int l, int r) { assert(0 <= l && l <= r && r <= _n); return sum(r) - sum(l); } private: int _n; std::vector data; U sum(int r) { U s = 0; while (r > 0) { s += data[r - 1]; r -= r & -r; } return s; } }; } // namespace atcoder using namespace std; using namespace atcoder; typedef long long ll; typedef unsigned long long ull; typedef pair iint; typedef pair llll; #define ALL(x) (x).begin(),(x).end() const ll zero = 0; const ll one = 1; const ll INF = 9223372036854775807; //10^18 const int inINF = 2147483647; //10^9 const ll MOD = 998244353; void Yes() {printf("Yes\n");} void No() {printf("No\n");} void YES() {printf("YES\n");} void NO() {printf("NO\n");} int main(){ int N; cin >> N; vector A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } map m; for (int i = 0; i < N; i++) { m[A[i]] = 1; } int ind = 1; for (auto v : m){ m[v.first] = ind; ind++; } fenwick_tree f1(N+2); fenwick_tree f2(N+2); vector B(N); for (ll i = 0; i < N; i++) { B[i] = m[A[i]]; } ll ans = 0; vector I(N), J(N), K(N), L(N), R(N); for (int i = 0; i < N; i++) { K[i] = f2.sum(B[i] + 1, N+1); L[i] = f1.sum(B[i] + 1, N+1); f1.add(B[i], 1); f2.add(B[i], f1.sum(B[i] + 1, N+1)); } fenwick_tree f3(N+2); fenwick_tree f4(N+2); for (int i = N-1; i >= 0; i--) { I[i] = f4.sum(0, B[i]); R[i] = f3.sum(0, B[i]); f3.add(B[i], 1); f4.add(B[i], f3.sum(0, B[i])); } for (ll i = 0; i < N; i++) { J[i] = L[i] * R[i] % MOD; I[i] %= MOD; K[i] %= MOD; } // for (int i = 0; i < N; i++) { // printf("%d %lld %d %lld %lld %lld\n", i, A[i], B[i], I[i], J[i], K[i]); // } // for (int i = 1; i < 4; i++) { // printf("%d %lld %lld %lld %lld\n", i, f1.sum(i, i+1), f2.sum(i, i+1), f3.sum(i, i+1), f4.sum(i, i+1)); // } for (ll i = 0; i < N; i++) { ans = (ans + A[i] * (I[i] + J[i] + K[i])) % MOD; } printf("%lld\n", ans); }