#include "bits/stdc++.h"
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x...)
#endif
constexpr int MOD = 998244353;
void md(long long &n) {
n = (n % MOD + MOD) % MOD;
}
void solve() {
int n;
cin >> n;
vector<long long> a(n);
for (long long &i : a) cin >> i;
vector<long long> dmx(n + 1), dmn(n + 1), pdp(n + 2);
dmx[0] = 1;
dmn[0] = 1;
vector<pair<int, int>> mx, mn;
mx.emplace_back(INT_MAX, 0);
mn.emplace_back(INT_MIN, 0);
for (int i = 1; i <= n; i++) {
while (a[i - 1] >= mx.back().first) {
mx.pop_back();
}
dmx[i] += (pdp[i] - pdp[mx.back().second + 1]) * a[i - 1];
md(dmx[i]);
dmx[i] += dmx[mx.back().second];
md(dmx[i]);
mx.emplace_back(a[i - 1], i);
debug(mx);
while (a[i - 1] <= mn.back().first) {
mn.pop_back();
}
dmn[i] += (pdp[i] - pdp[mn.back().second + 1]) * a[i - 1];
md(dmn[i]);
dmn[i] += dmn[mn.back().second];
md(dmn[i]);
mn.emplace_back(a[i - 1], i);
debug(mn);
pdp[i + 1] = pdp[i] + (dmx[i] - dmn[i]);
md(pdp[i + 1]);
}
debug(dmx);
debug(dmn);
debug(pdp);
long long ans = dmx[n] - dmn[n];
md(ans);
cout << ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int tests = 1;
// cin >> tests;
while (tests--) {
solve();
}
return 0;
}