#include <bits/stdc++.h>
using namespace std;
const long long N = 1002021, mod = 998244353;
long long n, ans, x[N], y[N], f[N];
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int main() {
  n = read();
  for (long long op, i = 1; i <= n; i++) {
    x[i] = read(), y[i] = read();
    op = read();
    long long lst = lower_bound(x + 1, x + i + 1, y[i]) - x;
    long long w = (f[i - 1] - f[lst - 1] + x[i] - y[i]) % mod;
    f[i] = (f[i - 1] + w) % mod;
    if (op) (ans += w) %= mod;
  }
  cout << ((ans + x[n] + 1) % mod + mod) % mod << endl;
  return 0;
}
