#include <bits/stdc++.h>
using namespace std;
long long i, i1, j, k, k1, t, n, m, res, flag[10], a, b;
long long x[200010], y[200010], s[200010], dp[200010][2], c[200010];
vector<array<long long, 2>> v;
vector<long long> adj[200010];
void fx(long long &x, long long p) {
  if ((x + p) % 2) x = (x + 998244353) % 1996488706;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> x[i] >> y[i] >> s[i];
    v.push_back({x[i], i});
  }
  x[n + 1] = x[n] + 1;
  for (i = 1; i <= n; i++) {
    array<long long, 2> o = {y[i], -(long long)1e18};
    auto it = lower_bound(v.begin(), v.end(), o);
    c[i] = (*it)[0] - y[i];
    y[i] = (*it)[1];
    adj[y[i]].push_back(i);
  }
  dp[n][0] = s[n] + 1;
  dp[n][1] = s[n];
  for (i = n - 1; i >= 1; i--) {
    dp[i][0] = (2 * dp[i + 1][0]) % 1996488706;
    for (auto u : adj[i + 1])
      dp[i][0] =
          (dp[i][0] - 2 * dp[u][1] + 2 * (long long)1996488706) % 1996488706;
    dp[i][0] = (dp[i][0] - (s[i] ^ 1) + 1996488706) % 1996488706;
    fx(dp[i][0], s[i] ^ 1);
    dp[i][1] = dp[i][0] / 2;
  }
  res = x[1] % 1996488706;
  for (i = 1; i <= n; i++) {
    res += (dp[i][1] * c[i]);
    res %= 1996488706;
    res +=
        ((dp[i][0] - dp[i][1] + 1996488706) * (x[i + 1] - x[i])) % 1996488706;
  }
  res %= 998244353;
  cout << res << "\n";
  return 0;
}
