description: An ant moves on the real line with constant speed of 1 unit per second. It starts at 0 and always moves to the right (so its position increases by 1 each second).
  
  There are n portals, the i-th of which is located at position x_i and teleports to position y_i < x_i. Each portal can be either active or inactive. The initial state of the i-th portal is determined by s_i: if s_i=0 then the i-th portal is initially inactive, if s_i=1 then the i-th portal is initially active. When the ant travels through a portal (i.e., when its position coincides with the position of a portal): 
  
    * if the portal is inactive, it becomes active (in this case the path of the ant is not affected); 
    * if the portal is active, it becomes inactive and the ant is instantly teleported to the position y_i, where it keeps on moving as normal. 
  
  
  
  How long (from the instant it starts moving) does it take for the ant to reach the position x_n + 1? It can be shown that this happens in a finite amount of time. Since the answer may be very large, compute it modulo 998 244 353.
  
  Input
  
  The first line contains the integer n (1≤ n≤ 2⋅ 10^5) — the number of portals.
  
  The i-th of the next n lines contains three integers x_i, y_i and s_i (1≤ y_i < x_i≤ 10^9, s_i∈\{0,1\}) — the position of the i-th portal, the position where the ant is teleported when it travels through the i-th portal (if it is active), and the initial state of the i-th portal.
  
  The positions of the portals are strictly increasing, that is x_1<x_2<⋅⋅⋅<x_n. It is guaranteed that the 2n integers x_1,   x_2,   ...,   x_n,   y_1,   y_2,   ...,   y_n are all distinct.
  
  Output
  
  Output the amount of time elapsed, in seconds, from the instant the ant starts moving to the instant it reaches the position x_n+1. Since the answer may be very large, output it modulo 998 244 353.
  
  Examples
  
  Input
  
  
  4
  3 2 0
  6 5 1
  7 4 0
  8 1 1
  
  
  Output
  
  
  23
  
  
  Input
  
  
  1
  454971987 406874902 1
  
  
  Output
  
  
  503069073
  
  
  Input
  
  
  5
  243385510 42245605 0
  644426565 574769163 0
  708622105 208990040 0
  786625660 616437691 0
  899754846 382774619 0
  
  
  Output
  
  
  899754847
  
  
  Input
  
  
  5
  200000000 100000000 1
  600000000 400000000 0
  800000000 300000000 0
  900000000 700000000 1
  1000000000 500000000 0
  
  
  Output
  
  
  3511295
  
  Note
  
  Explanation of the first sample: The ant moves as follows (a curvy arrow denotes a teleporting, a straight arrow denotes normal movement with speed 1 and the time spent during the movement is written above the arrow). $$$ 0 \stackrel{6}{\longrightarrow} 6 \leadsto 5 \stackrel{3}{\longrightarrow} 8 \leadsto 1 \stackrel{2}{\longrightarrow} 3 \leadsto 2 \stackrel{4}{\longrightarrow} 6 \leadsto 5 \stackrel{2}{\longrightarrow} 7 \leadsto 4 \stackrel{2}{\longrightarrow} 6 \leadsto 5 \stackrel{4}{\longrightarrow} 9  Notice that the total time is 6+3+2+4+2+2+4=23$$$.
  
  Explanation of the second sample: The ant moves as follows (a curvy arrow denotes a teleporting, a straight arrow denotes normal movement with speed 1 and the time spent during the movement is written above the arrow). $$$ 0 \stackrel{454971987}{\longrightarrow} 454971987 \leadsto 406874902 \stackrel{48097086}{\longrightarrow} 454971988  Notice that the total time is 454971987+48097086=503069073$$$.
  
  Explanation of the third sample: Since all portals are initially off, the ant will not be teleported and will go straight from 0 to x_n+1=899754846+1=899754847.
solution: #include <bits/stdc++.h>
  using namespace std;
  void dbg_out() { cerr << endl; }
  template <typename Head, typename... Tail>
  void dbg_out(Head H, Tail... T) {
    cerr << ' ' << H;
    dbg_out(T...);
  }
  template <const int &MOD>
  struct _m_int {
    int val;
    _m_int(int64_t v = 0) {
      if (v < 0) v = v % MOD + MOD;
      if (v >= MOD) v %= MOD;
      val = int(v);
    }
    _m_int(uint64_t v) {
      if (v >= MOD) v %= MOD;
      val = int(v);
    }
    _m_int(int v) : _m_int(int64_t(v)) {}
    _m_int(unsigned v) : _m_int(uint64_t(v)) {}
    explicit operator int() const { return val; }
    explicit operator unsigned() const { return val; }
    explicit operator int64_t() const { return val; }
    explicit operator uint64_t() const { return val; }
    explicit operator double() const { return val; }
    explicit operator long double() const { return val; }
    _m_int &operator+=(const _m_int &other) {
      val -= MOD - other.val;
      if (val < 0) val += MOD;
      return *this;
    }
    _m_int &operator-=(const _m_int &other) {
      val -= other.val;
      if (val < 0) val += MOD;
      return *this;
    }
    static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
      return unsigned(x % m);
      unsigned x_high = unsigned(x >> 32), x_low = unsigned(x);
      unsigned quot, rem;
      asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
      return rem;
    }
    _m_int &operator*=(const _m_int &other) {
      val = fast_mod(uint64_t(val) * other.val);
      return *this;
    }
    _m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
    friend _m_int operator+(const _m_int &a, const _m_int &b) {
      return _m_int(a) += b;
    }
    friend _m_int operator-(const _m_int &a, const _m_int &b) {
      return _m_int(a) -= b;
    }
    friend _m_int operator*(const _m_int &a, const _m_int &b) {
      return _m_int(a) *= b;
    }
    friend _m_int operator/(const _m_int &a, const _m_int &b) {
      return _m_int(a) /= b;
    }
    _m_int &operator++() {
      val = val == MOD - 1 ? 0 : val + 1;
      return *this;
    }
    _m_int &operator--() {
      val = val == 0 ? MOD - 1 : val - 1;
      return *this;
    }
    _m_int operator++(int) {
      _m_int before = *this;
      ++*this;
      return before;
    }
    _m_int operator--(int) {
      _m_int before = *this;
      --*this;
      return before;
    }
    _m_int operator-() const { return val == 0 ? 0 : MOD - val; }
    friend bool operator==(const _m_int &a, const _m_int &b) {
      return a.val == b.val;
    }
    friend bool operator!=(const _m_int &a, const _m_int &b) {
      return a.val != b.val;
    }
    friend bool operator<(const _m_int &a, const _m_int &b) {
      return a.val < b.val;
    }
    friend bool operator>(const _m_int &a, const _m_int &b) {
      return a.val > b.val;
    }
    friend bool operator<=(const _m_int &a, const _m_int &b) {
      return a.val <= b.val;
    }
    friend bool operator>=(const _m_int &a, const _m_int &b) {
      return a.val >= b.val;
    }
    static const int SAVE_INV = int(1e6) + 5;
    static _m_int save_inv[SAVE_INV];
    static void prepare_inv() {
      for (int64_t p = 2; p * p <= MOD; p += p % 2 + 1) assert(MOD % p != 0);
      save_inv[0] = 0;
      save_inv[1] = 1;
      for (int i = 2; i < SAVE_INV; i++)
        save_inv[i] = save_inv[MOD % i] * (MOD - MOD / i);
    }
    _m_int inv() const {
      if (save_inv[1] == 0) prepare_inv();
      if (val < SAVE_INV) return save_inv[val];
      _m_int product = 1;
      int v = val;
      while (v >= SAVE_INV) {
        product *= MOD - MOD / v;
        v = MOD % v;
      }
      return product * save_inv[v];
    }
    _m_int pow(int64_t p) const {
      if (p < 0) return inv().pow(-p);
      _m_int a = *this, result = 1;
      while (p > 0) {
        if (p & 1) result *= a;
        p >>= 1;
        if (p > 0) a *= a;
      }
      return result;
    }
    friend ostream &operator<<(ostream &os, const _m_int &m) {
      return os << m.val;
    }
  };
  template <const int &MOD>
  _m_int<MOD> _m_int<MOD>::save_inv[_m_int<MOD>::SAVE_INV];
  const int MOD = 998244353;
  using mint = _m_int<MOD>;
  const int MXN = 1e5 + 5, INF = 1e9 + 5;
  void solve() {
    int N;
    cin >> N;
    vector<int> loc(N + 1), teleport(N + 1), state(N + 1);
    for (int i = 1; i <= N; i++) cin >> loc[i] >> teleport[i] >> state[i];
    loc[0] = loc[1];
    teleport[0] = teleport[1];
    state[0] = state[1];
    vector<int> closest(N + 1);
    for (int i = 1; i <= N; i++)
      closest[i] =
          upper_bound(loc.begin() + 1, loc.end(), teleport[i]) - loc.begin();
    closest[0] = closest[1];
    vector<mint> time_taken(N + 1, 0);
    mint ans = loc[1];
    for (int i = 1; i <= N; i++) {
      time_taken[i] = loc[closest[i]] - teleport[i] + time_taken[i - 1] -
                      time_taken[closest[i] - 1];
      ans += loc[i] - loc[i - 1];
      if (state[i] == 1) ans += time_taken[i];
      if (i < N) time_taken[i] += loc[i + 1] - loc[i];
      time_taken[i] += time_taken[i - 1];
    }
    cout << ans + 1;
  }
  signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int TC = 1;
    while (TC--) solve();
  }
  