#include <bits/stdc++.h>
using namespace std;
long long x[200001], y[200001], dp[200001], n;
bool s[200001];
long long find_upper_bound(long long l, long long r, long long value) {
  long long mid, index = 1;
  while (l <= r) {
    mid = l + (r - l) / 2;
    if (x[mid] >= value) {
      index = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  return index;
}
int main() {
  int t;
  t = 1;
  while (t--) {
    dp[0] = 0;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> x[i] >> y[i] >> s[i];
    long long ans = (x[n] + 1) % 998244353, temp;
    for (int i = 1; i <= n; i++) {
      temp = find_upper_bound(1, i, y[i]);
      dp[i] = x[i] - y[i];
      if (temp != i)
        dp[i] = (dp[i] + (dp[i - 1] - dp[temp - 1] + 998244353) % 998244353) %
                998244353;
      if (s[i]) ans = (ans + dp[i]) % 998244353;
      dp[i] = (dp[i] + dp[i - 1]) % 998244353;
    }
    cout << ans << endl;
  }
}
