#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<pair<int, int>> a(n);
  int b[n];
  long long dp[n];
  set<pair<int, int>> s;
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second >> b[i];
    s.insert({a[i].first, i});
  }
  long long pre[n + 1];
  pre[0] = 0;
  for (int i = 0; i < n; i++) {
    dp[i] = a[i].first - a[i].second;
    auto it = s.lower_bound({a[i].second, 0});
    dp[i] = (dp[i] + pre[i] - pre[(it->second)] + 998244353) % 998244353;
    pre[i + 1] = (pre[i] + dp[i]) % 998244353;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (b[i]) {
      ans = (ans + dp[i]) % 998244353;
    }
  }
  cout << (ans + a[n - 1].first + 1) % 998244353;
}
