#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
inline int read() {
  int x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}
long long n, l[1000005], r[1000005], e[1000005], b[1000005], a[1000005], ans;
const long long mod = 998244353;
int main() {
  n = read();
  for (int i = 1; i <= n; i++) l[i] = read(), r[i] = read(), e[i] = read();
  l[n + 1] = l[n] + 1;
  a[0] = l[1];
  for (int i = 1; i <= n; i++) {
    int L = 0, R = n, x = 0;
    while (L <= R) {
      int mid = (L + R) >> 1;
      if (l[mid] > r[i])
        x = mid, R = mid - 1;
      else
        L = mid + 1;
    }
    b[i] = x;
  }
  for (int i = 1; i <= n; i++)
    a[i] = (a[i - 1] + l[b[i]] - r[i] + a[i - 1] - a[b[i] - 1] + l[i + 1] -
            l[i] + 3 * mod) %
           mod;
  for (int i = 1; i <= n; i++) {
    if (e[i])
      ans = (ans + (a[i] - a[i - 1]) + mod) % mod;
    else
      ans = (ans + (l[i + 1] - l[i]) + mod) % mod;
  }
  printf("%lld\n", (ans + l[1]) % mod);
  return 0;
}
