description: You are given an array of n positive integers a_1, a_2, …, a_n. Your task is to calculate the number of arrays of n positive integers b_1, b_2, …, b_n such that: 
  
    * 1 ≤ b_i ≤ a_i for every i (1 ≤ i ≤ n), and 
    * b_i ≠ b_{i+1} for every i (1 ≤ i ≤ n - 1). 
  
  
  
  The number of such arrays can be very large, so print it modulo 998 244 353.
  
  Input
  
  The first line contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of the array a.
  
  The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9).
  
  Output
  
  Print the answer modulo 998 244 353 in a single line.
  
  Examples
  
  Input
  
  
  3
  2 2 2
  
  
  Output
  
  
  2
  
  Input
  
  
  2
  2 3
  
  
  Output
  
  
  4
  
  Input
  
  
  3
  1 1 1
  
  
  Output
  
  
  0
  
  Note
  
  In the first test case possible arrays are [1, 2, 1] and [2, 1, 2].
  
  In the second test case possible arrays are [1, 2], [1, 3], [2, 1] and [2, 3].
solution: #include <bits/stdc++.h>
  using namespace std;
  const int N = 2e6 + 99, mod = 998244353;
  int n, t, a[N], dp[N][2], last[N], lazy[N];
  struct node {
    int sum[2], sumd[2];
    node() { sum[0] = sum[1] = sumd[0] = sumd[1] = 0; }
  } seg[N];
  void shift(int, int, int);
  node merge(node u, node v) {
    node ans;
    for (int i = 0; i < 2; i++) {
      ans.sum[i] = (u.sum[i] + v.sum[i]) % mod;
      ans.sumd[i] = (u.sumd[i] + v.sumd[i]) % mod;
    }
    return ans;
  }
  void read() {
    cin >> n;
    for (int i = 1; i < n + 1; i++) cin >> a[i];
    last[1] = 0;
    for (int i = 2; i < n + 1; i++) {
      last[i] = i - 1;
      while (last[i] != 0 && a[last[i]] > a[i]) {
        last[i] = last[last[i]];
      }
    }
  }
  void change(int L, int R, int val, int id = 1, int l = 0, int r = n) {
    if (r <= L || R <= l) return;
    if (L <= l && r <= R) {
      lazy[id] = val;
      for (int i = 0; i < 2; i++) {
        seg[id].sum[i] = 1ll * val * seg[id].sumd[i] % mod;
      }
      return;
    }
    int mid = (l + r) >> 1;
    shift(id, l, r);
    change(L, R, val, id * 2 + 0, l, mid);
    change(L, R, val, id * 2 + 1, mid, r);
    seg[id] = merge(seg[id * 2 + 0], seg[id * 2 + 1]);
  }
  void upd(int x, int id = 1, int l = 0, int r = n) {
    if (r <= x || x < l) return;
    if (l + 1 == r && l == x) {
      if (x & 1) {
        seg[id].sumd[0] = dp[x][1];
        seg[id].sumd[1] = dp[x][0];
      } else {
        seg[id].sumd[0] = dp[x][0];
        seg[id].sumd[1] = dp[x][1];
      }
      return;
    }
    int mid = (l + r) / 2;
    shift(id, l, r);
    upd(x, id * 2 + 0, l, mid);
    upd(x, id * 2 + 1, mid, r);
    seg[id] = merge(seg[id * 2 + 0], seg[id * 2 + 1]);
  }
  void shift(int id, int l, int r) {
    if (lazy[id] == -1) return;
    int mid = (l + r) / 2;
    change(l, mid, lazy[id], id * 2 + 0, l, mid);
    change(mid, r, lazy[id], id * 2 + 1, mid, r);
    lazy[id] = -1;
  }
  void solve() {
    dp[0][0] = 1;
    upd(0);
    for (int i = 1; i < n + 1; i++) {
      change(last[i], i, a[i]);
      if (i & 1) {
        dp[i][0] = seg[1].sum[0];
        dp[i][1] = seg[1].sum[1];
      } else {
        dp[i][0] = seg[1].sum[1];
        dp[i][1] = seg[1].sum[0];
      }
      if (i == n) break;
      upd(i);
    }
    cout << (dp[n][0] - dp[n][1] + mod) % mod;
  }
  int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    read();
    solve();
  }
  