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>
  constexpr int kMod = 998244353;
  template <typename T>
  T add(T a, T b) {
    return (a + b) % kMod;
  }
  template <typename T, typename... Args>
  T add(T a, T b, Args... args) {
    return add(add(a, b), args...);
  }
  template <typename T>
  T sub(T a, T b) {
    return (a + kMod - b) % kMod;
  }
  template <typename T, typename... Args>
  T sub(T a, T b, Args... args) {
    return sub(sub(a, b), args...);
  }
  template <typename T>
  T mul(T a, T b) {
    return a * static_cast<long long>(b) % kMod;
  }
  template <typename T, typename... Args>
  T mul(T a, T b, Args... args) {
    return mul(mul(a, b), args...);
  }
  template <typename T>
  void Add(T &a, T b) {
    a = add(a, b);
  }
  template <typename T, typename... Args>
  void Add(T &a, T b, Args... args) {
    Add(a, add(b, args...));
  }
  template <typename T>
  void Sub(T &a, T b) {
    a = sub(a, b);
  }
  template <typename T, typename... Args>
  void Sub(T &a, T b, Args... args) {
    Sub(a, add(b, args...));
  }
  template <typename T>
  void Mul(T &a, T b) {
    a = mul(a, b);
  }
  template <typename T, typename... Args>
  void Mul(T &a, T b, Args... args) {
    Mul(a, mul(b, args...));
  }
  template <typename T1, typename T2>
  T1 Ksm(T1 a, T2 b) {
    T1 s = 1;
    while (b) {
      if (b & 1) Mul(s, a);
      Mul(a, a);
      b >>= 1;
    }
    return s;
  }
  int fac[1000001], inv[1000001], iv[1000001];
  void Init(int n) {
    fac[0] = 1;
    for (int i = 1; i <= n; i++) fac[i] = mul(fac[i - 1], i);
    inv[n] = Ksm(fac[n], kMod - 2);
    for (int i = n - 1; i >= 0; i--) inv[i] = mul(inv[i + 1], i + 1);
    iv[0] = iv[1] = 1;
    for (int i = 2; i <= n; i++) iv[i] = mul(kMod - kMod / i, iv[kMod % i]);
  }
  int C(int x, int y) {
    if (x < y || y < 0) return 0;
    return mul(fac[x], inv[x - y], inv[y]);
  }
  template <typename T>
  void checkmax(T &x, T y) {
    if (x < y) x = y;
  }
  template <typename T>
  void checkmin(T &x, T y) {
    if (x > y) x = y;
  }
  template <class T, typename = decltype(std::declval<T>().begin()),
            typename = decltype(std::declval<T>().end()),
            typename = typename std::enable_if<!std::is_base_of<
                std::string, typename std::decay<T>::type>::value>::type,
            typename value_type = typename T::value_type>
  std::ostream &operator<<(std::ostream &out, const T &container) {
    for (auto it = container.begin(); it != container.end(); ++it)
      out << *it << ' ';
    return out;
  }
  std::ostream &operator<<(std::ostream &out, __int128_t x) {
    static int s[101], top;
    top = 0;
    while (x) s[top++] = x % 10, x /= 10;
    for (int i = top - 1; i >= 0; i--) out << s[i];
    return out;
  }
  int n, a[500001], s[500001], stk[500001], top, s2[500001], pre[500001];
  int main(int argc, char const *argv[]) {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr), std::cout.tie(nullptr);
    std::cin >> n;
    for (int i = 1; i <= n; i++) std::cin >> a[i];
    for (int i = 1; i <= n; i++) {
      while (top && a[stk[top]] > a[i]) top--;
      pre[i] = stk[top];
      stk[++top] = i;
    }
    s[0] = s2[0] = 1;
    for (int i = 1; i <= n; i++) {
      int f = sub(s2[i - 1], pre[i] ? s2[pre[i] - 1] : 0);
      if (~i & 1) f = (kMod - f) % kMod;
      s[i] = mul(f, a[i]);
      int dis = i - pre[i];
      if (pre[i]) {
        if (dis & 1)
          Sub(s[i], s[pre[i]]);
        else
          Add(s[i], s[pre[i]]);
      }
      if (i & 1)
        s2[i] = sub(s2[i - 1], s[i]);
      else
        s2[i] = add(s2[i - 1], s[i]);
    }
    std::cout << s[n];
    return 0;
  }
  