#include<bits/stdc++.h> #define re register #define int long long
usingnamespace std;
constint N = 3e5 + 10,mod = 998244353; int n; int arr[N]; int tp1,st1[N]; int tp2,st2[N]; int Max[N],Min[N],dp[N];
inlineintread(){ int r = 0,w = 1; char c = getchar(); while (c < '0' || c > '9'){ if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9'){ r = (r << 3) + (r << 1) + (c ^ 48); c = getchar(); } return r * w; }
inlineintAdd(int a,int b){ return (a + b) % mod; }
inlineintSub(int a,int b){ return ((a - b) % mod + mod) % mod; }
inlineintMul(int a,int b){ return a * b % mod; }
signedmain(){ dp[0] = 1; n = read(); for (re int i = 1;i <= n;i++) arr[i] = read(); for (re int i = 1;i <= n;i++){ while (tp1 && arr[st1[tp1]] <= arr[i]) tp1--; while (tp2 && arr[st2[tp2]] >= arr[i]) tp2--; if (tp1) Max[i] = Add(Max[st1[tp1]],Mul(Sub(dp[i - 1],dp[st1[tp1] - 1]),arr[i])); else Max[i] = Mul(dp[i - 1],arr[i]);//当 tp1 和 tp2 为 0 时需要特殊处理,避免越界 if (tp2) Min[i] = Add(Min[st2[tp2]],Mul(Sub(dp[i - 1],dp[st2[tp2] - 1]),arr[i])); else Min[i] = Mul(dp[i - 1],arr[i]); dp[i] = Add(dp[i - 1],Sub(Max[i],Min[i])); st1[++tp1] = st2[++tp2] = i; } printf("%lld",Sub(dp[n],dp[n - 1])); return0; }