#include // #include using namespace std; // using namespace atcoder; using ll = long long; using ull = unsigned long long; using P = pair; #define rep(i,n) for(ll i = 0;i < (ll)n;i++) #define ALL(x) (x).begin(),(x).end() #define MOD 1000000007 template class Segtree{ public: using F = function; int n; F f; T ti; vector dat; Segtree(){}; Segtree(F f,T ti): f(f),ti(ti){} void init(int N){ n = 1; while(n < N)n <<= 1; dat.assign(n << 1,ti); } void build(const vector &v){ int N = v.size(); init(N); rep(i,N)dat[n+i] = v[i]; for(int i = n-1;i >= 0;i--)dat[i] = f(dat[(i << 1)|0],dat[(i << 1)|1]); } void set(int k,T x){ dat[k+=n] = x; while(k >>= 1){ dat[k] = f(dat[(k << 1)|0],dat[(k << 1)|1]); } } T get(int l,int r){ T vl = ti,vr = ti; for(l += n,r += n;l < r;l >>= 1,r >>= 1){ if(l & 1)vl = f(vl,dat[l++]); if(r & 1)vr = f(dat[--r],vr); } return f(vl,vr); } }; const int mod = 998244353; int main(){ int n; cin >> n; vector v(n); vector w(n); rep(i,n)cin >> v[i],w[i] = v[i]; sort(ALL(w)); w.erase(unique(ALL(w)),w.end()); map mp; int m = w.size(); rep(i,m)mp[w[i]] = i; auto f = [&](pair a,pair b){return pair{(a.first+b.first)%mod,(a.second+b.second)%mod};}; Segtree> one(f,{0,0}),two(f,{0,0}),three(f,{0,0}); one.build(vector>(m,{0,0})); two.build(vector>(m,{0,0})); three.build(vector>(m,{0,0})); rep(i,n){ int k = mp[v[i]]; { pair x = one.get(k,k+1); x.first += v[i];x.second++; x.first %= mod; one.set(k,x); } { pair x = two.get(k,k+1); pair y = one.get(k+1,m); two.set(k,{(x.first+y.first+y.second*v[i]%mod)%mod,(x.second+y.second)%mod}); } { pair x = three.get(k,k+1); pair y = two.get(k+1,m); three.set(k,{(x.first+y.first+y.second*v[i]%mod)%mod,(x.second+y.second)%mod}); } } cout << three.get(0,m).first << "\n"; return 0; }