#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using matr = vector<vector<ll>>;
using dbl = long double;
using ui = unsigned int;
using ld = long double;
mt19937 rnd;
ll random(ll limit) {
  uniform_int_distribution<ll> uniform_dist(0, limit);
  return uniform_dist(rnd);
}
ll mod = 998244353;
ll bpow(ll a, ll p) {
  ll res = 1;
  while (p > 0) {
    if (p % 2) res *= a;
    p /= 2;
    a *= a;
    res %= mod;
    a %= mod;
  }
  return res;
}
ll inv(ll a) { return bpow(a, mod - 2); }
ll gcd(ll a, ll b) {
  if (!b) return a;
  return gcd(b, a % b);
}
void lol(ll& a) {
  a %= mod;
  a += mod;
  a %= mod;
}
void solve() {
  int n;
  cin >> n;
  vector<ll> x(n), y(n), s(n);
  vector<vector<ll>> kek;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> s[i];
    kek.push_back({y[i], 0, i});
    kek.push_back({x[i], 1, i});
  }
  sort(kek.begin(), kek.end());
  map<ll, ll> pref;
  ll cur = 0;
  ll ans = 0;
  ll last = 0;
  for (int i = 0; i < kek.size(); i++) {
    if (kek[i][1] == 0) {
      cur += kek[i][0] - last;
      lol(cur);
      pref[kek[i][0]] = cur;
    } else {
      cur += kek[i][0] - last;
      ll beg = y[kek[i][2]];
      ll len = (cur - pref[beg]);
      lol(len);
      if (s[kek[i][2]] == 0) {
        ans += mod - len;
        lol(ans);
      }
      pref[kek[i][0]] = (cur + len) % mod;
      cur += len;
      lol(cur);
    }
    last = kek[i][0];
  }
  ll ans1 = pref[kek[kek.size() - 1][0]] + 1;
  ans1 %= mod;
  ans += ans1;
  ans %= mod;
  cout << ans << '\n';
}
int main() {
  int t;
  t = 1;
  for (int i = 0; i < t; i++) {
    solve();
  }
}
