#include #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using PL = pair; const long long mod = 998244353LL; vector node; vector nodf; int sz = 1; void init(int n){ while (sz < n) sz *= 2; node.resize(2 * sz - 1); nodf.resize(2 * sz - 1); rep(i,2 * sz - 1) {node[i] = 0LL; nodf[i] = 0LL;} } void update(int a, ll b){ a += sz - 1; node[a] += b; node[a] %= mod; while (a > 0){ a = (a - 1) / 2; node[a] = node[2 * a + 1] + node[2 * a + 2]; node[a] %= mod; } } void updatf(int a, ll b){ a += sz - 1; nodf[a] += b; nodf[a] %= mod; while (a > 0){ a = (a - 1) / 2; nodf[a] = nodf[2 * a + 1] + nodf[2 * a + 2]; nodf[a] %= mod; } } ll query1(int a, int b, int k=0, int l=0, int r=sz){ if (b <= l || r <= a) return 0LL; if (a <= l && r <= b) return (node[k] % mod); ll vl = query1(a,b,2*k+1,l,(l+r)/2); ll vr = query1(a,b,2*k+2,(l+r)/2,r); return ((vl + vr) % mod); } ll query2(int a, int b, int k=0, int l=0, int r=sz){ if (b <= l || r <= a) return 0LL; if (a <= l && r <= b) return (nodf[k] % mod); ll vl = query2(a,b,2*k+1,l,(l+r)/2); ll vr = query2(a,b,2*k+2,(l+r)/2,r); return ((vl + vr) % mod); } int main(){ int n; cin >> n; vector ar(n); rep(i,n) cin >> ar[i]; reverse(ar.begin(),ar.end()); vector al = ar; sort(al.begin(),al.end()); al.erase(unique(al.begin(),al.end()),al.end()); map mp; int si = al.size(); rep(i,si) mp[al[i]] = i; init(si); vector dp(n); rep(i,n){ int l = mp[ar[i]]; ll a = query1(0,l,0,0,sz); ll b = query2(0,l,0,0,sz); dp[i].first = a; dp[i].second = (b + (ar[i] * a % mod)) % mod; update(l,1LL); updatf(l,ar[i]); } vector ep(n); init(si); rep(i,n){ int l = mp[ar[i]]; ll a = query1(0,l,0,0,sz); ll b = query2(0,l,0,0,sz); ep[i].first = a; ep[i].second = (b + (ar[i] * a % mod)) % mod; update(l,dp[i].first); updatf(l,dp[i].second); } ll ans = 0LL; rep(i,n){ ans += ep[i].second; ans %= mod; } cout << ans << endl; }