#include <bits/stdc++.h>
using namespace std;
long long int mod = 998244353;
long long int s[200005];
map<long long int, long long int> mp;
long long int x[200005];
long long int y[200005];
long long int dp[200005];
long long int p[200005];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int n;
  cin >> n;
  set<long long int> se;
  for (long long int i = 1; i <= n; i++) {
    cin >> x[i] >> y[i] >> s[i];
    se.insert(x[i]);
    mp[x[i]] = i;
  }
  long long int ans = x[n] + 1;
  for (long long int i = 1; i <= n; i++) {
    dp[i] += x[i] - y[i];
    ans %= mod;
    long long int j = mp[*se.lower_bound(y[i])];
    dp[i] += p[i - 1] - p[j - 1] + mod;
    dp[i] %= mod;
    p[i] = p[i - 1] + dp[i];
    p[i] %= mod;
  }
  for (long long int i = 1; i <= n; i++) {
    if (s[i] == 1) {
      ans += dp[i];
      ans %= mod;
    }
  }
  cout << ans;
}
