#include #include #include using namespace std; using namespace atcoder; using mint = modint998244353; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000001 #define Inf64 1000000000000000000LL vector a; mint ans = 0; void dfs(int l,int r){ if(l==r)return; if(r-l==1){ ans += mint(a[l]).pow(2); return; } int m = (l+r)/2; dfs(l,m); dfs(m+1,r); vector> t; vector ms; { int mini = Inf32,maxi = -Inf32; for(int i=m;i=l;i--){ mini = min(mini,a[i]); maxi = max(maxi,a[i]); t.push_back({mini,maxi,0}); ms.push_back(maxi); } } sort(ms.begin(),ms.end()); ms.erase(unique(ms.begin(),ms.end()),ms.end()); sort(t.rbegin(),t.rend()); vector fc(2,fenwick_tree(ms.size())); auto fs = fc; rep(i,t.size()){ int pos = lower_bound(ms.begin(),ms.end(),t[i][1])-ms.begin(); mint tt = fs[t[i][2]^1].sum(pos,ms.size()); tt += fc[t[i][2]^1].sum(0,pos)*t[i][1]; tt *= t[i][0]; ans += tt; fc[t[i][2]].add(pos,1); fs[t[i][2]].add(pos,t[i][1]); } } int main(){ int n; cin>>n; a.resize(n); rep(i,n) cin>>a[i]; dfs(0,n); cout<