#include #include #include #include #include using namespace std; int lsb(int x){ return x & (-x); } typedef long long ll; #define MODX 998244353 class FenwickTree{ public: ll * B; int n; FenwickTree(int n){ this->n = n; this->B = new ll[n+3]; for(int i = 0; i <= n+1; ++i) this->B[i] = 0; } ll getsum(int index){ ll sum = 0; index = index + 1; while(index > 0){ sum = (this->B[index] + sum) % MODX; index -= lsb(index); } return sum; } void update(int index, ll val){ index = index + 1; while(index <= n){ this->B[index] = (this->B[index] + val ) %MODX; index += lsb(index); } } }; ll add(ll x, ll y){ return (x+y)%MODX; } ll mul(ll x, ll y){ return (x*y)%MODX; } void CoordinateCompress(vector u, vector & v){ sort(u.begin(),u.end()); map mpitoind; int n = u.size(); for(int i = 0; i< n; ++i) mpitoind[u[i]] = i; for(int i = 0; i < n; ++i) v[i] = mpitoind[v[i]]; } int main() { int n; cin >> n; vector a; vector b; for(int i = 0; i< n; ++i){ int x; cin >> x; a.push_back(x); b.push_back(x); } CoordinateCompress(a,a); FenwickTree * presum = new FenwickTree(n+2); FenwickTree * f = new FenwickTree(n+2); FenwickTree * g = new FenwickTree(n+2); FenwickTree * c = new FenwickTree(n+2); FenwickTree * gp = new FenwickTree(n+2); for(int i = n-1; i >= 0; --i){ ll nf = 1, ng = 0, nc = 0, ngp = 0; if(a[i] - 1 >= 0){ ll frequency = f->getsum(a[i]-1); ll pres = presum->getsum(a[i]-1); ng = add( mul(frequency, b[i]) , pres); nc = frequency; ll frequency2 = c->getsum(a[i]-1); ll pres2 = g->getsum(a[i]-1); ngp = add( mul(frequency2, b[i]) , pres2); } presum->update(a[i], b[i]); f->update(a[i], nf); g->update(a[i], ng); c->update(a[i], nc); gp->update(a[i], ngp); } cout << gp->getsum(n); return 0; }