#include <bits/stdc++.h>
using namespace std;
long long n, i, ans, j, mod = 998244353;
vector<long long> x, y, s, d, p;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  x.resize(n);
  y.resize(n);
  s.resize(n);
  d.resize(n + 1);
  p.resize(n + 1);
  for (i = 0; i < n; i++) cin >> x[i] >> y[i] >> s[i];
  for (i = 0; i < n; i++) {
    j = (long long)(lower_bound(x.begin(), x.end(), y[i]) - x.begin());
    d[i] += ((p[i] - p[j]) % mod + mod) % mod;
    d[i] += ((x[i] - y[i]) % mod + mod) % mod;
    d[i] %= mod;
    p[i + 1] = ((p[i] + d[i]) % mod + mod) % mod;
    p[i + 1] %= mod;
  }
  ans = ((x[n - 1] + 1) % mod + mod) % mod;
  for (i = 0; i < n; i++)
    if (s[i]) ans = ((ans + d[i]) % mod + mod) % mod;
  cout << ans;
}
