#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long MOD = 998244353;
inline void read(int &x) {
  x = 0;
  char c = getchar();
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
}
long long quick_mod(long long A, long long B) {
  long long ret = 1;
  A %= MOD;
  while (B) {
    if (B & 1) ret = ret * A % MOD;
    B >>= 1;
    A = A * A % MOD;
  }
  return ret;
}
inline void chkmin(long long &x, long long y) { x = min(x, y); }
inline void chkmax(long long &x, long long y) { x = max(x, y); }
inline void add(long long &x, long long y) { x = (x + y) % MOD; }
inline long long rev(long long x) { return quick_mod(x, MOD - 2); }
inline int lowbit(int x) { return x & (-x); }
const int MAXN = 2e5 + 10;
int N;
long long X[MAXN], Y[MAXN];
int S[MAXN];
long long dp[MAXN];
long long prv[MAXN];
int main(void) {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) scanf("%lld%lld%d", X + i, Y + i, S + i);
  long long ans = 0;
  for (int i = 0; i < N; ++i) {
    int it = lower_bound(X, X + N, Y[i]) - X;
    dp[i] = (X[i] - Y[i] + prv[i] - prv[it] + 4 * MOD) % MOD;
    if (S[i]) add(ans, dp[i]);
    prv[i + 1] = (prv[i] + dp[i]) % MOD;
  }
  add(ans, X[N - 1] + 1);
  printf("%lld", ans);
  return 0;
}
