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 MAXN = 5e5 + 10, MOD = 998244353;
  template <int MOD_>
  struct modnum {
    static constexpr int MOD = MOD_;
    static_assert(MOD_ > 0, "MOD must be positive");
  
   private:
    using long long = long long;
    int v;
    static int minv(int a, int m) {
      a %= m;
      assert(a);
      return a == 1 ? 1 : int(m - long long(minv(m, a)) * long long(m) / a);
    }
  
   public:
    modnum() : v(0) {}
    modnum(long long v_) : v(int(v_ % MOD)) {
      if (v < 0) v += MOD;
    }
    explicit operator int() const { return v; }
    friend std::ostream& operator<<(std::ostream& out, const modnum& n) {
      return out << int(n);
    }
    friend std::istream& operator>>(std::istream& in, modnum& n) {
      long long v_;
      in >> v_;
      n = modnum(v_);
      return in;
    }
    friend bool operator==(const modnum& a, const modnum& b) {
      return a.v == b.v;
    }
    friend bool operator!=(const modnum& a, const modnum& b) {
      return a.v != b.v;
    }
    modnum inv() const {
      modnum res;
      res.v = minv(v, MOD);
      return res;
    }
    friend modnum inv(const modnum& m) { return m.inv(); }
    modnum neg() const {
      modnum res;
      res.v = v ? MOD - v : 0;
      return res;
    }
    friend modnum neg(const modnum& m) { return m.neg(); }
    modnum operator-() const { return neg(); }
    modnum operator+() const { return modnum(*this); }
    modnum& operator++() {
      v++;
      if (v == MOD) v = 0;
      return *this;
    }
    modnum& operator--() {
      if (v == 0) v = MOD;
      v--;
      return *this;
    }
    modnum& operator+=(const modnum& o) {
      v -= MOD - o.v;
      v = (v < 0) ? v + MOD : v;
      return *this;
    }
    modnum& operator-=(const modnum& o) {
      v -= o.v;
      v = (v < 0) ? v + MOD : v;
      return *this;
    }
    modnum& operator*=(const modnum& o) {
      v = int(long long(v) * long long(o.v) % MOD);
      return *this;
    }
    modnum& operator/=(const modnum& o) { return *this *= o.inv(); }
    friend modnum operator++(modnum& a, int) {
      modnum r = a;
      ++a;
      return r;
    }
    friend modnum operator--(modnum& a, int) {
      modnum r = a;
      --a;
      return r;
    }
    friend modnum operator+(const modnum& a, const modnum& b) {
      return modnum(a) += b;
    }
    friend modnum operator-(const modnum& a, const modnum& b) {
      return modnum(a) -= b;
    }
    friend modnum operator*(const modnum& a, const modnum& b) {
      return modnum(a) *= b;
    }
    friend modnum operator/(const modnum& a, const modnum& b) {
      return modnum(a) /= b;
    }
  };
  using num = modnum<MOD>;
  const long long NONE = -1e18 + 10;
  struct T {
    long long lzy = NONE;
    num sum = 0;
    T *l, *r;
    long long tl, tr;
    T(long long _tl, long long _tr) : tl(_tl), tr(_tr) {
      lzy = NONE;
      l = r = nullptr;
    }
    void extend() {
      if (l || r || tl == tr) return;
      long long tm = (tl + tr) / 2;
      l = new T(tl, tm), r = new T(tm + 1, tr);
    }
    num get_sum() { return lzy == NONE ? sum : num(lzy) * num(tr - tl + 1); }
    void push() {
      if (lzy == NONE) return;
      sum = get_sum();
      if (l || r) {
        l->lzy = lzy, r->lzy = lzy;
      }
      lzy = NONE;
    }
    void set(long long ql, long long qr, long long x) {
      if (ql <= tl && tr <= qr) {
        lzy = x;
        return;
      }
      extend();
      push();
      if (ql <= l->tr) l->set(ql, qr, x);
      if (qr >= r->tl) r->set(ql, qr, x);
      sum = l->get_sum() + r->get_sum();
    }
  } * t;
  int n;
  long long a[MAXN];
  const int L = 0, R = 1e9 + 10;
  void solve() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i], --a[i];
    t = new T(L, R);
    int mul = 1;
    num add = 0;
    auto get_sum = [&]() -> num { return (t->sum) * mul + add * num(R - L + 1); };
    t->set(L, R, 0);
    for (int i = 0; i < n; i++) {
      if (!i) {
        t->set(0, a[i], 1);
      } else {
        num x = get_sum();
        mul *= -1, add *= num(-1);
        add += x;
        t->set(a[i] + 1, R, (0 - int(add)) / mul);
      }
    }
    cout << get_sum() << '\n';
  }
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T = 1;
    while (T--) solve();
  }
  