#include <bits/stdc++.h>
using namespace std;
long long get_time() {
  return chrono::duration_cast<chrono::nanoseconds>(
             chrono::steady_clock::now().time_since_epoch())
      .count();
}
template <typename T1, typename T2>
string print_iterable(T1 begin_iter, T2 end_iter, int counter) {
  bool done_something = false;
  stringstream res;
  res << "[";
  for (; begin_iter != end_iter and counter; ++begin_iter) {
    done_something = true;
    counter--;
    res << *begin_iter << ", ";
  }
  string str = res.str();
  if (done_something) {
    str.pop_back();
    str.pop_back();
  }
  str += "]";
  return str;
}
template <typename S, typename T>
ostream& operator<<(ostream& out, const pair<S, T>& p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  out << "[";
  for (int i = 0; i < (int)v.size(); i++) {
    out << v[i];
    if (i != (int)v.size() - 1) out << ", ";
  }
  out << "]";
  return out;
}
template <class TH>
void _dbg(const char* name, TH val) {
  clog << name << ": " << val << endl;
}
template <class TH, class... TA>
void _dbg(const char* names, TH curr_val, TA... vals) {
  while (*names != ',') clog << *names++;
  clog << ": " << curr_val << ", ";
  _dbg(names + 1, vals...);
}
const long long mod = 998244353;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> x(n);
  vector<int> s(n);
  vector<long long> q(n);
  vector<long long> sum_q(n + 1, 0);
  for (int i = 0; i < n; i++) {
    int y;
    cin >> x[i] >> y >> s[i];
    int j = lower_bound(x.begin(), x.begin() + i, y) - x.begin();
    q[i] = x[i] - y + sum_q[i] - sum_q[j];
    q[i] %= mod;
    sum_q[i + 1] = sum_q[i] + q[i];
    sum_q[i + 1] %= mod;
  }
  long long ans = x[n - 1] + 1;
  for (int i = 0; i < n; i++)
    if (s[i]) ans += q[i];
  ans %= mod;
  if (ans < 0) ans += mod;
  cout << ans << "\n";
}
