#include using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) begin(v),end(v) template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using pii = pair; constexpr ll INF = 1ll<<30; constexpr ll longINF = 1ll<<60; constexpr ll MOD = 998244353; constexpr bool debug = false; //---------------------------------// template struct ModInt { static_assert(M > 0); public: using value_type = int; using calc_type = std::int_fast64_t; private: value_type val_; public: constexpr ModInt(calc_type val = 0) : val_(val < 0 ? (val % M + M) % M : val % M) {} constexpr const value_type & val() const noexcept { return val_; } constexpr static decltype(M) mod() noexcept { return M; } explicit constexpr operator bool() const noexcept { return val_; } constexpr bool operator !() const noexcept { return !static_cast(*this); } constexpr ModInt operator +() const noexcept { return ModInt(*this); } constexpr ModInt operator -() const noexcept { return ModInt(-val_); } constexpr ModInt operator ++(int) noexcept { ModInt res = *this; ++*this; return res; } constexpr ModInt operator --(int) noexcept { ModInt res = *this; --*this; return res; } constexpr ModInt & operator ++() noexcept { val_ = val_ + 1 == M ? 0 : val_ + 1; return *this; } constexpr ModInt & operator --() noexcept { val_ = val_ == 0 ? M - 1 : val_ - 1; return *this; } constexpr ModInt & operator +=(const ModInt & rhs) noexcept { val_ += val_ < M - rhs.val_ ? rhs.val_ : rhs.val_ - M; return *this; } constexpr ModInt & operator -=(const ModInt & rhs) noexcept { val_ += val_ >= rhs.val_ ? -rhs.val_ : M - rhs.val_; return *this; } constexpr ModInt & operator *=(const ModInt & rhs) noexcept { val_ = static_cast(val_) * rhs.val_ % M; return *this; } constexpr ModInt & operator /=(const ModInt & rhs) noexcept { return *this *= rhs.inv(); } friend constexpr ModInt operator +(const ModInt & lhs, const ModInt & rhs) noexcept { return ModInt(lhs) += rhs; } friend constexpr ModInt operator -(const ModInt & lhs, const ModInt & rhs) noexcept { return ModInt(lhs) -= rhs; } friend constexpr ModInt operator *(const ModInt & lhs, const ModInt & rhs) noexcept { return ModInt(lhs) *= rhs; } friend constexpr ModInt operator /(const ModInt & lhs, const ModInt & rhs) noexcept { return ModInt(lhs) /= rhs; } friend constexpr bool operator ==(const ModInt & lhs, const ModInt & rhs) noexcept { return lhs.val_ == rhs.val_; } friend constexpr bool operator !=(const ModInt & lhs, const ModInt & rhs) noexcept { return !(lhs == rhs); } friend std::ostream & operator <<(std::ostream & os, const ModInt & rhs) { return os << rhs.val_; } friend std::istream & operator >>(std::istream & is, ModInt & rhs) { calc_type x; is >> x; rhs = ModInt(x); return is; } constexpr ModInt pow(calc_type n) const noexcept { ModInt res = 1, x = val_; if (n < 0) { x = x.inv(); n = -n; } while (n) { if (n & 1) res *= x; x *= x; n >>= 1; } return res; } constexpr ModInt inv() const noexcept { value_type a = val_, a1 = 1, a2 = 0, b = M, b1 = 0, b2 = 1; while (b > 0) { value_type q = a / b, r = a % b; value_type nb1 = a1 - q * b1, nb2 = a2 - q * b2; a = b; b = r; a1 = b1; b1 = nb1; a2 = b2; b2 = nb2; } assert(a == 1); return a1; } }; using mint = ModInt; template struct BinaryIndexedTree { using value_type = T; using const_reference = const value_type &; using F = std::function; using size_type = std::size_t; BinaryIndexedTree(size_type n, const F & f, const_reference id_elem) : n(n), f(f), id_elem(id_elem) { node.resize(n + 1, id_elem); } size_type size() const noexcept { return n; } void add(size_type i, const_reference x) { assert(i < size()); ++i; for (; i <= size(); i += i & -i) node[i] = f(node[i], x); } // [0, i) value_type sum(size_type i) const { assert(i <= size()); value_type res = id_elem; for (; i > 0; i -= i & -i) res = f(res, node[i]); return res; } // sum[0, r] <= x を満たす最小の r を返す (存在しなければ size()) size_type lower_bound(const_reference x) const { size_type res = 0; size_type s = id_elem, w = 1; while (w < size()) w <<= 1; for (; w > 0; w >>= 1) { if (res + w <= size()) { value_type cur = f(s, node[res + w]); if (cur < x) { res += w; s = cur; } } } return res; } private: size_type n; F f; value_type id_elem; std::vector node; }; int main() { int N; cin >> N; vector A(N); REP(i, N) scanf("%d", &A[i]); vector cx; auto compress = [](auto &&v, auto &&cv) -> void { for (auto &&u : v) cv.emplace_back(u); cv.emplace_back(INF); cv.emplace_back(-INF); std::sort(std::begin(cv), std::end(cv)); cv.erase(std::unique(std::begin(cv), std::end(cv)), std::end(cv)); for (auto &&u : v) u = std::lower_bound(std::begin(cv), std::end(cv), u) - std::begin(cv); }; compress(A, cx); using P = pair; BinaryIndexedTree

rbit(cx.size(), [](const P & x, const P & y) { return P{x.first + y.first, x.second + y.second}; }, P{0, 0}); auto lbit = rbit; FOR(i, 2, N) rbit.add(A[i], P{1, cx[A[i]]}); lbit.add(A[0], P{1, cx[A[0]]}); mint ans = 0; FOR(i, 1, N - 1) { auto l1 = lbit.sum(cx.size()), l2 = lbit.sum(A[i] + 1); auto r1 = rbit.sum(A[i]); ans += (l1.second - l2.second) * r1.first; ans += (l1.first - l2.first) * r1.second; ans += (l1.first - l2.first) * r1.first * cx[A[i]]; lbit.add(A[i], {1, cx[A[i]]}); rbit.add(A[i + 1], {-1, -cx[A[i + 1]]}); } cout << ans << endl; }