#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline", \
                     "03")
const long long maxn = 1000010;
const long long mod2 = 1000000007;
const long long mod = 998244353;
const long double eps = 1e-9;
const long long inf = ((1ll << 31ll) - 1ll);
const long long INF = 1ll * mod * mod;
const long double pi = acos(-1);
long long qpow(long long b, long long e) {
  if (!e) return 1;
  if (e & 1) return qpow(b, e - 1) * b % mod;
  long long pwur = qpow(b, e >> 1);
  return pwur * pwur % mod;
}
long long modinv(long long x) { return qpow(x, mod - 2); }
bool isprime(long long x) {
  for (long long i = 2; i * i <= x; i++)
    if (x % i == 0) return false;
  return true;
}
mt19937_64 r(time(0));
long long rng(long long a, long long b) {
  return uniform_int_distribution<long long>(a, b)(r);
}
long long n, cnt[maxn], sub[maxn], sub2[maxn];
vector<pair<pair<long long, long long>, long long>> v;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.setf(ios::fixed);
  cout.precision(10);
  srand(time(NULL));
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long x, y, second;
    cin >> x >> y >> second;
    v.push_back({{x, y}, second});
  }
  for (long long i = n - 1; i >= 0; i--) {
    cnt[i] = v[i].second;
  }
  long long curr = 0, ans = (v[n - 1].first.first + 1) % mod;
  for (long long i = n - 1; i >= 0; i--) {
    cnt[i] = (cnt[i] + curr) % mod;
    curr = (curr + cnt[i]) % mod;
    ans = (ans + cnt[i] * (v[i].first.first - v[i].first.second)) % mod;
    curr = (curr - sub[i] + mod) % mod;
    long long id =
        lower_bound(
            v.begin(), v.end(),
            (pair<pair<long long, long long>, long long>){
                (pair<long long, long long>){v[i].first.second, 0}, 0}) -
        v.begin();
    if (id == i) {
      curr = (curr - cnt[i] + mod) % mod;
    }
    sub[id] = (sub[id] + cnt[i]) % mod;
  }
  cout << ans << '\n';
  return 0;
}
