#include #include using namespace std; using namespace atcoder; #define ll long long using mint = modint998244353; ll mod = 998244353; ll op(ll a, ll b) { return (a + b) % mod; } ll e() { return 0; } void solve() { int n; cin >> n; vector a(n); for(int i=0; i> a[i]; segtree rsum(n), rcnt(n), lsum(n), lcnt(n); vector m_sum(n, 0), m_cnt(n, 0); vector id(n); iota(id.begin(), id.end(), 0); sort(id.begin(), id.end(), [&](int i, int j) { if (a[i] != a[j]) return a[i] < a[j]; return i < j; }); for (auto &i: id) { m_sum[i] = rsum.prod(i, n); m_cnt[i] = rcnt.prod(i, n); rsum.set(i, a[i]); rcnt.set(i, 1); } ll answer = 0; sort(id.begin(), id.end(), [&](int i, int j) { if (a[i] != a[j]) return a[i] > a[j]; return i > j; }); for (auto &i: id) { auto s = lsum.prod(0, i); auto c = lcnt.prod(0, i); if (c > 0 && m_cnt[i] > 0) { answer += s * m_cnt[i] % mod; answer %= mod; answer += c * m_sum[i] % mod; answer %= mod; answer += (c * m_cnt[i] % mod) * a[i] % mod; answer %= mod; } lsum.set(i, a[i]); lcnt.set(i, 1); } cout << answer << "\n"; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int t = 1; // std::cin >> t; while (t--) solve(); return 0; }