#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
int main() {
  int n;
  cin >> n;
  n++;
  vector<long long> x(n), y(n), f(n), dp(n);
  for (int i = 1; i < n; i++) {
    cin >> x[i] >> y[i] >> f[i];
  }
  for (int i = 1; i < n; i++) {
    int lb = lower_bound(x.begin(), x.end(), y[i]) - x.begin();
    dp[i] = (dp[i - 1] + dp[i - 1] - dp[lb - 1] + x[i] - y[i] + mod) % mod;
  }
  long long ans = (x[n - 1] + 1) % mod;
  for (int i = 1; i < n; i++) {
    if (f[i]) ans = (ans + dp[i] - dp[i - 1] + mod) % mod;
  }
  cout << ans << endl;
  return 0;
}
