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>
  bool time_seed = 0;
  using namespace std;
  using namespace std::chrono;
  struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
  };
  struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  };
  void show_vi(vector<int> t) {
    for (int i = 0; i < t.size(); i++) {
      cout << t[i] << ",";
    }
    cout << '\n';
  }
  void show_vp(vector<pair<int, int>> t) {
    for (int i = 0; i < t.size(); i++) {
      cout << t[i].first << " " << t[i].second << "::";
    }
    cout << '\n';
  }
  void show_vd(vector<double> t) {
    for (int i = 0; i < t.size(); i++) {
      cout << t[i] << ",";
    }
    cout << '\n';
  }
  void show_vll(vector<long long int> t) {
    for (int i = 0; i < t.size(); i++) {
      cout << t[i] << ",";
    }
    cout << '\n';
  }
  void show_vt(vector<tuple<long long, long long>> t) {
    for (int i = 0; i < t.size(); i++) {
      cout << get<0>(t[i]) << " " << get<1>(t[i]) << "::";
    }
    cout << '\n';
  }
  void show_vb(vector<bool> t) {
    for (int i = 0; i < t.size(); i++) {
      cout << t[i] << " ";
    }
    cout << '\n';
  }
  void show_vvi(vector<vector<int>> ans) {
    cout << "---------------------------" << '\n';
    for (int i = 0; i < ans.size(); i++) {
      show_vi(ans[i]);
    }
    cout << '\n';
  }
  int randd() {
    timeval start_time;
    long milli_time, seconds, useconds;
    gettimeofday(&start_time, NULL);
    seconds = start_time.tv_sec;
    useconds = start_time.tv_usec;
    milli_time = ((seconds)*1000 + useconds / 1000.0);
    if (time_seed == 0) {
      time_seed = 1;
      srand(milli_time);
    }
    int k = rand();
    return k;
  }
  void show_vvvi(vector<vector<vector<int>>> ans) {
    for (int i = 0; i < ans.size(); i++) {
      cout << "******************" << '\n';
      show_vvi(ans[i]);
    }
    cout << '\n';
  }
  void show_vvb(vector<vector<bool>> ans) {
    cout << "---------------------------" << '\n';
    for (int i = 0; i < ans.size(); i++) {
      show_vb(ans[i]);
    }
    cout << '\n';
  }
  void show_vs(vector<string> A) {
    for (int i = 0; i < A.size(); i++) {
      cout << A[i] << "::";
    }
    cout << '\n';
  }
  void show_vc(vector<char> A) {
    for (int i = 0; i < A.size(); i++) {
      cout << A[i];
    }
    cout << '\n';
  }
  void show_vvc(vector<vector<char>> A) {
    cout << "---------------------------" << '\n';
    for (int i = 0; i < A.size(); i++) {
      show_vc(A[i]);
    }
  }
  void show_vvs(vector<vector<string>> A) {
    for (int i = 0; i < A.size(); i++) {
      show_vs(A[i]);
    }
  }
  void show_vvvs(vector<vector<vector<string>>> ans) {
    for (int i = 0; i < ans.size(); i++) {
      cout << "******************" << '\n';
      show_vvs(ans[i]);
    }
    cout << '\n';
  }
  void show_um(unordered_map<int, int> &map) {
    unordered_map<int, int>::iterator it;
    it = map.begin();
    while (it != map.end()) {
      cout << it->first << " " << it->second << "::";
      it++;
    }
    cout << '\n';
  }
  long long int ncr(long long int n, long long int r) {
    if (r > n - r) r = n - r;
    long long int C[r + 1];
    memset(C, 0, sizeof(C));
    C[0] = 1;
    for (long long int i = 1; i <= n; i++) {
      for (int j = min(i, r); j > 0; j--) {
        C[j] = (C[j] + C[j - 1]);
        C[j] %= 1000000007;
      }
    }
    return C[r];
  }
  vector<long long int> prime_factors(long long int n) {
    vector<long long int> ans;
    while (n % 2 == 0) {
      ans.push_back(2);
      n = n / 2;
    }
    for (int i = 3; i <= sqrt(n); i = i + 2) {
      while (n % i == 0) {
        ans.push_back(i);
        n = n / i;
      }
    }
    if (n > 2) ans.push_back(n);
    return ans;
  }
  long long int pow_m(long long int a, long long int b) {
    if (b == 1) return a % 1000000007;
    if (b == 0) return 1 % 1000000007;
    long long int k = (long long int)pow_m(a, b / 2);
    if (b % 2) {
      return (((k * k) % 1000000007) * a) % 1000000007;
    } else
      return (((k * k) % 1000000007));
  }
  long long int poww(long long int a, long long int b) {
    if (b == 1) return a;
    if (b == 0) return 1;
    long long int k = poww(a, b / 2);
    if (b % 2) {
      return k * k * a;
    } else
      return k * k;
  }
  int modInverse(int a, int m) { return pow_m(a, m - 2); }
  bool isPrime(int n) {
    if (n <= 1) return false;
    if (n <= 3) return true;
    if (n % 2 == 0 || n % 3 == 0) return false;
    for (int i = 5; i * i <= n; i = i + 6)
      if (n % i == 0 || n % (i + 2) == 0) return false;
    return true;
  }
  string int_to_hex(int a) {
    if (a == 0) return "0";
    string ans;
    while (a) {
      int k = a % 16;
      if (k <= 9) {
        ans += ('0' + k);
      } else {
        ans += ('A' + (k - 10));
      }
      a /= 16;
    }
    reverse(ans.begin(), ans.end());
    return ans;
  }
  long long int ncrr(long long int a, long long int b) {
    long long int ans = 1;
    if (b > a / 2) {
      b = a - b;
    }
    for (int i = 0; i < b; i++) {
      ans *= a;
      a--;
      ans %= 1000000007;
    }
    for (int i = 1; i <= b; i++) {
      ans *= (modInverse(i, 1000000007));
      ans %= 1000000007;
    }
    return ans;
  }
  void init_code() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
  }
  class KMP {
   public:
    vector<int> LPS(string s) {
      int n = s.size();
      vector<int> lps(n, 0);
      int len = 0;
      int i = 1;
      while (i < n) {
        if (s[i] == s[len]) {
          lps[i++] = ++len;
        } else {
          if (len != 0) {
            len = lps[len - 1];
          } else {
            lps[i++] = 0;
          }
        }
      }
      return lps;
    }
    vector<int> match(string A, string B) {
      auto lps = LPS(B);
      vector<int> ans;
      int n = A.size();
      int m = B.size();
      int i = 0;
      int j = 0;
      while (i < n) {
        if (A[i] == B[j]) {
          i++;
          j++;
        }
        if (j == m) {
          ans.push_back(i - j);
          j = lps[j - 1];
        } else if (i < n && A[i] != B[j]) {
          if (j == 0)
            i++;
          else
            j = lps[j - 1];
        }
      }
      return ans;
    }
  };
  class fast_segtree {
   public:
    static const long long int N = 2e5 + 15;
    long long int n;
    long long int t[2 * N];
    void build() {
      for (long long int i = n - 1; i > 0; --i) t[i] = t[i << 1] + t[i << 1 | 1];
    }
    void modify(long long int p, long long int value) {
      for (t[p += n] = value; p > 1; p >>= 1) t[p >> 1] = t[p] + t[p ^ 1];
    }
    long long int query(long long int l, long long int r) {
      long long int res = 0;
      for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l & 1) res += t[l++];
        if (r & 1) res += t[--r];
      }
      return res;
    }
    fast_segtree(vector<long long int> &A) {
      n = A.size();
      for (long long int i = 0; i < A.size(); i++) t[i + n] = A[i];
      build();
    };
    fast_segtree(long long int n1) {
      n = n1;
      for (long long int i = 0; i < n; i++) t[i + n] = 0;
      build();
    };
  };
  class segtree {
   public:
    vector<long long int> A;
    long long int n;
    segtree(long long int n1) {
      n = n1;
      A.resize(4 * n + 5);
    };
    void sett(long long int id, long long int s, long long int e,
              long long int idx, long long int val) {
      if (idx < s || idx > e) return;
      A[id] += val;
      if (s == e) return;
      long long int mid = (s + e) / 2;
      sett(2 * id, s, mid, idx, val);
      sett(2 * id + 1, mid + 1, e, idx, val);
    }
    long long int gett(long long int id, long long int s, long long int e,
                       long long int l, long long int r) {
      if (s >= l && e <= r) return A[id];
      if (min(e, r) < max(s, l)) return 0;
      long long int mid = (s + e) / 2;
      long long int ans = 0;
      ans += gett(2 * id, s, mid, l, r);
      ans += gett(2 * id + 1, mid + 1, e, l, r);
      return ans;
    }
    void sett(long long int id, long long int val) { sett(1, 0, n - 1, id, val); }
    long long int gett(long long int l, long long int r) {
      return gett(1, 0, n - 1, l, r - 1);
    }
  };
  vector<long long int> djikstra(int sr, vector<vector<pair<int, int>>> &gr) {
    int n = gr.size();
    vector<long long int> ans(n, 1e15);
    priority_queue<pair<int, int>> pq;
    pq.push({0, sr});
    while (pq.size()) {
      auto top = pq.top();
      pq.pop();
      int id = top.second;
      int val = -top.second;
      if (ans[id] > val) {
        ans[id] = val;
        for (int j = 0; j < gr[id].size(); j++) {
          long long int new_dis = -val - gr[id][j].second;
          int des = gr[id][j].first;
          if (ans[des] > -new_dis) {
            pq.push({new_dis, des});
          }
        }
      }
    }
    return ans;
  }
  const int max_sz = 2e5 + 5;
  int X[max_sz + 1], Y[max_sz + 1], S[max_sz + 1], pref[max_sz + 1];
  int modd = 998244353;
  long long int dfs(int i) {
    long long int ans = 0;
    int s = 0;
    int e = i;
    while (s < e) {
      int m = (s + e) / 2;
      if (X[m] < Y[i]) {
        s = m + 1;
      } else
        e = m;
    }
    ans += (X[i] - Y[i]) + 1;
    ans += (pref[i] - pref[s]);
    ans += (s - i);
    ans %= modd;
    pref[i + 1] = pref[i] + ans;
    pref[i + 1] %= modd;
    return ans;
  }
  void help() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> X[i] >> Y[i] >> S[i];
    }
    pref[0] = 0;
    for (int i = 0; i < n; i++) dfs(i);
    int prev = 0;
    long long int ans = 0;
    for (int i = 0; i < n; i++) {
      ans += (X[i] - prev);
      if (S[i]) {
        ans += (pref[i + 1] - pref[i]);
        prev = X[i] + 1;
      } else {
        prev = X[i];
      }
      ans %= modd;
    }
    if (S[n - 1] == 0) ans++;
    ans %= modd;
    if (ans < 0) ans += modd;
    cout << ans << '\n';
  }
  int main() {
    init_code();
    int t = 1;
    for (int i = 1; i <= t; i++) {
      help();
    }
    return 0;
  }
  