#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
const long long INF = 1e9;
void solve() {
  long long n;
  cin >> n;
  vector<long long> ans(n + 1);
  vector<pair<long long, long long>> xx(n + 1);
  vector<long long> ss(n);
  xx[0] = {0, -1000000000};
  for (long long i = 0; i < n; ++i) {
    cin >> xx[i + 1].first >> xx[i + 1].second >> ss[i];
  }
  for (long long i = 0; i < n; ++i) {
    auto j =
        lower_bound(xx.begin(), xx.end(), make_pair(xx[i + 1].second, -1ll)) -
        xx.begin();
    long long k = j - 1;
    if (k == i) {
      ans[i + 1] = ans[i] + (xx[i + 1].first - xx[i].first) +
                   (xx[i + 1].first - xx[i + 1].second);
    } else {
      ans[i + 1] = ans[i] + (xx[i + 1].first - xx[i].first) -
                   (ans[k] + (xx[i + 1].second - xx[k].first)) +
                   (xx[i + 1].first - xx[i].first) + ans[i];
    }
    ans[i + 1] %= MOD;
    ans[i + 1] += MOD;
    ans[i + 1] %= MOD;
  }
  vector<long long> ans2(n + 1);
  for (long long i = 0; i < n; ++i) {
    auto j =
        lower_bound(xx.begin(), xx.end(), make_pair(xx[i + 1].second, -1ll)) -
        xx.begin();
    long long k = j - 1;
    if (ss[i]) {
      if (k == i) {
        ans2[i + 1] = (xx[i + 1].first - xx[i + 1].second) +
                      (xx[i + 1].first - xx[i].first) + ans2[i];
      } else {
        ans2[i + 1] = ans[i] + (xx[i + 1].first - xx[i].first) -
                      (ans[k] + (xx[i + 1].second - xx[k].first)) +
                      (xx[i + 1].first - xx[i].first) + ans2[i];
      }
    } else {
      ans2[i + 1] = (xx[i + 1].first - xx[i].first) + ans2[i];
    }
    ans2[i + 1] %= MOD;
    ans2[i + 1] += MOD;
    ans2[i + 1] %= MOD;
  }
  cout << (ans2[n] + 1) % MOD << "\n";
}
int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0),
      cout << fixed << setprecision(20);
  long long t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
