#include <bits/stdc++.h>
using namespace std;
long long useful(vector<long long>& nums) {
  long long n = nums.size();
  vector<long long> dp1(n + 1, 0);
  vector<long long> dp2(n + 1, 0);
  for (long long i = 1; i <= n; i++) {
    dp1[i] = max(dp1[i - 1], dp2[i - 1] + nums[i - 1]);
    dp2[i] = max(dp2[i - 1], dp1[i - 1] - nums[i - 1]);
  }
  return dp1[n];
}
long long power(long long x, long long y, long long m = 1000000007) {
  x = x % m;
  if (y == 0) return 1LL;
  if (y % 2) return (x * power((x * x) % m, y / 2, m)) % m;
  return power((x * x) % m, y / 2, m);
}
vector<long long> sieve(long long n) {
  long long* arr = new long long[n + 1]();
  vector<long long> vect;
  for (long long i = 2; i <= n; i++)
    if (arr[i] == 0) {
      vect.push_back(i);
      for (long long j = i * i; j <= n; j += i) arr[j] = 1;
    }
  delete[] arr;
  return vect;
}
bool comp(long long a, long long b) { return a > b; }
bool comp_pair(pair<long long, long long> a, pair<long long, long long> b) {
  return a.first > b.first;
}
const long long m = 998244353;
void solve() {
  long long n;
  cin >> n;
  long long x[n], y[n], s[n];
  for (long long i = 0; i < n; i++) cin >> x[i] >> y[i] >> s[i];
  long long pre[n];
  long long suf[n + 1];
  memset(suf, 0, sizeof(suf));
  suf[0] = 0;
  for (long long i = 0; i < n; i++) {
    long long ind = lower_bound(x, x + i, y[i]) - x;
    pre[i] = (x[i] - y[i] + suf[i] - suf[ind]) % m;
    suf[i + 1] = (suf[i] + pre[i]) % m;
  }
  long long ans = (x[n - 1] + 1) % m;
  for (long long i = 0; i < n; i++) {
    if (s[i] == 1) {
      ans = (ans + pre[i]) % m;
    }
  }
  cout << (ans + m) % m << endl;
}
signed main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long t = 1;
  while (t--) solve();
  return 0;
}
