#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
using ll = long long;
using R = long double;
const R EPS = 1e-9L;
inline int sgn(const R &r) { return (r > EPS) - (r < -EPS); }
inline R sq(R x) { return sqrt(max(x, 0.0L)); }
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const pid_t pid = getpid();
template <uint32_t MOD>
class static_modint {
 public:
  static_modint() : data(0) {}
  static_modint(int32_t y) : data(y >= 0 ? y % MOD : MOD - (-y) % MOD) {}
  static_modint(int64_t y) : data(y >= 0 ? y % MOD : MOD - (-y) % MOD) {}
  static static_modint raw(int32_t y) {
    static_modint ret;
    ret.data = y;
    return ret;
  }
  static_modint &operator+=(const static_modint &that) {
    if ((data += that.data) >= MOD) {
      data -= MOD;
    }
    return *this;
  }
  static_modint &operator-=(const static_modint &that) {
    if ((data += MOD - that.data) >= MOD) {
      data -= MOD;
    }
    return *this;
  }
  static_modint &operator*=(const static_modint &that) {
    data = 1LL * data * that.data % MOD;
    return *this;
  }
  static_modint &operator/=(const static_modint &that) {
    return *this *= static_modint(std::get<1>(extgcd(that.data, int(MOD))));
  }
  static_modint &operator%=(const static_modint &that) {
    data %= that.data;
    return *this;
  }
  static_modint &operator+=(const int that) {
    return *this += static_modint(that);
  }
  static_modint &operator-=(const int that) {
    return *this -= static_modint(that);
  }
  static_modint &operator*=(const int that) {
    return *this *= static_modint(that);
  }
  static_modint &operator/=(const int that) {
    return *this /= static_modint(that);
  }
  static_modint &operator%=(const int that) {
    return *this %= static_modint(that);
  }
  bool operator<(const static_modint &that) { return data < that.data; }
  bool operator>(const static_modint &that) { return data > that.data; }
  bool operator<=(const static_modint &that) { return data <= that.data; }
  bool operator>=(const static_modint &that) { return data >= that.data; }
  bool operator!=(const static_modint &that) { return data != that.data; }
  bool operator==(const static_modint &that) { return data == that.data; }
  unsigned val() const { return data; }
  unsigned mod() const { return MOD; }
  static_modint operator+(const static_modint &that) const {
    return static_modint(*this) += that;
  }
  static_modint operator-(const static_modint &that) const {
    return static_modint(*this) -= that;
  }
  static_modint operator*(const static_modint &that) const {
    return static_modint(*this) *= that;
  }
  static_modint operator/(const static_modint &that) const {
    return static_modint(*this) /= that;
  }
  static_modint operator%(const static_modint &that) const {
    return static_modint(*this) %= that;
  }
  static_modint operator+(const int that) const {
    return static_modint(*this) += that;
  }
  static_modint operator-(const int that) const {
    return static_modint(*this) -= that;
  }
  static_modint operator*(const int that) const {
    return static_modint(*this) *= that;
  }
  static_modint operator/(const int that) const {
    return static_modint(*this) /= that;
  }
  static_modint operator%(const int that) const {
    return static_modint(*this) %= that;
  }
  static_modint operator=(const int that) {
    return *this = static_modint(that);
  }
  friend std::istream &operator>>(std::istream &is, static_modint &that) {
    int64_t tmp;
    is >> tmp;
    that = static_modint(tmp);
    return is;
  }
  friend std::ostream &operator<<(std::ostream &os, const static_modint &that) {
    return os << that.data;
  }
  static_modint power(ll n) const {
    static_modint b = raw(1), a = *this;
    while (n) {
      if (n & 1) {
        b *= a;
      }
      a *= a;
      n >>= 1;
    }
    return b;
  }

 private:
  unsigned data;
  inline std::tuple<int32_t, int32_t, int32_t> extgcd(int32_t a,
                                                      int32_t b) const {
    if (b == 0) {
      return std::make_tuple(a, 1, 0);
    }
    const auto ret = extgcd(b, a % b);
    std::swap(std::get<1>(ret), std::get<2>(ret));
    std::get<2>(ret) -= a / b * std::get<1>(ret);
    return ret;
  }
};
using modint998244353 = static_modint<998244353>;
using mint = modint998244353;
int main(void) {
  int n;
  cin >> n;
  vector<int> x(n + 1), y(n + 1), s(n + 1);
  vector<mint> sum(n + 1);
  for (int i = int(1); i < int(n + 1); ++i) {
    cin >> x[i] >> y[i] >> s[i];
  }
  mint ans = x[n] + 1;
  for (int i = int(1); i < int(n + 1); ++i) {
    const int j = lower_bound(begin(x), end(x), y[i]) - begin(x);
    const mint cur = mint::raw(x[i] - y[i]) + sum[i - 1] - sum[j - 1];
    sum[i] = sum[i - 1] + cur;
    if (s[i]) {
      ans += cur;
    }
  }
  cout << ans << endl;
  return 0;
}

### Prompt

Create a solution in CPP for the following problem:

An ant moves on the real line with constant speed of 1 unit per second. It starts at 0 and always moves to the right (so its position increases by 1 each second).

There are n portals, the i-th of which is located at position x_i and teleports to position y_i < x_i. Each portal can be either active or inactive. The initial state of the i-th portal is determined by s_i: if s_i=0 then the i-th portal is initially inactive, if s_i=1 then the i-th portal is initially active. When the ant travels through a portal (i.e., when its position coincides with the position of a portal): 

  * if the portal is inactive, it becomes active (in this case the path of the ant is not affected); 
  * if the portal is active, it becomes inactive and the ant is instantly teleported to the position y_i, where it keeps on moving as normal. 



How long (from the instant it starts moving) does it take for the ant to reach the position x_n + 1? It can be shown that this happens in a finite amount of time. Since the answer may be very large, compute it modulo 998 244 353.

Input

The first line contains the integer n (1≤ n≤ 2⋅ 10^5) — the number of portals.

The i-th of the next n lines contains three integers x_i, y_i and s_i (1≤ y_i < x_i≤ 10^9, s_i∈\{0,1\}) — the position of the i-th portal, the position where the ant is teleported when it travels through the i-th portal (if it is active), and the initial state of the i-th portal.

The positions of the portals are strictly increasing, that is x_1<x_2<⋅⋅⋅<x_n. It is guaranteed that the 2n integers x_1,   x_2,   ...,   x_n,   y_1,   y_2,   ...,   y_n are all distinct.

Output

Output the amount of time elapsed, in seconds, from the instant the ant starts moving to the instant it reaches the position x_n+1. Since the answer may be very large, output it modulo 998 244 353.

Examples

Input


4
3 2 0
6 5 1
7 4 0
8 1 1


Output


23


Input


1
454971987 406874902 1


Output


503069073


Input


5
243385510 42245605 0
644426565 574769163 0
708622105 208990040 0
786625660 616437691 0
899754846 382774619 0


Output


899754847


Input


5
200000000 100000000 1
600000000 400000000 0
800000000 300000000 0
900000000 700000000 1
1000000000 500000000 0


Output


3511295

Note

Explanation of the first sample: The ant moves as follows (a curvy arrow denotes a teleporting, a straight arrow denotes normal movement with speed 1 and the time spent during the movement is written above the arrow). $$$ 0 \stackrel{6}{\longrightarrow} 6 \leadsto 5 \stackrel{3}{\longrightarrow} 8 \leadsto 1 \stackrel{2}{\longrightarrow} 3 \leadsto 2 \stackrel{4}{\longrightarrow} 6 \leadsto 5 \stackrel{2}{\longrightarrow} 7 \leadsto 4 \stackrel{2}{\longrightarrow} 6 \leadsto 5 \stackrel{4}{\longrightarrow} 9  Notice that the total time is 6+3+2+4+2+2+4=23$$$.

Explanation of the second sample: The ant moves as follows (a curvy arrow denotes a teleporting, a straight arrow denotes normal movement with speed 1 and the time spent during the movement is written above the arrow). $$$ 0 \stackrel{454971987}{\longrightarrow} 454971987 \leadsto 406874902 \stackrel{48097086}{\longrightarrow} 454971988  Notice that the total time is 454971987+48097086=503069073$$$.

Explanation of the third sample: Since all portals are initially off, the ant will not be teleported and will go straight from 0 to x_n+1=899754846+1=899754847.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
using ll = long long;
using R = long double;
const R EPS = 1e-9L;
inline int sgn(const R &r) { return (r > EPS) - (r < -EPS); }
inline R sq(R x) { return sqrt(max(x, 0.0L)); }
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const pid_t pid = getpid();
template <uint32_t MOD>
class static_modint {
 public:
  static_modint() : data(0) {}
  static_modint(int32_t y) : data(y >= 0 ? y % MOD : MOD - (-y) % MOD) {}
  static_modint(int64_t y) : data(y >= 0 ? y % MOD : MOD - (-y) % MOD) {}
  static static_modint raw(int32_t y) {
    static_modint ret;
    ret.data = y;
    return ret;
  }
  static_modint &operator+=(const static_modint &that) {
    if ((data += that.data) >= MOD) {
      data -= MOD;
    }
    return *this;
  }
  static_modint &operator-=(const static_modint &that) {
    if ((data += MOD - that.data) >= MOD) {
      data -= MOD;
    }
    return *this;
  }
  static_modint &operator*=(const static_modint &that) {
    data = 1LL * data * that.data % MOD;
    return *this;
  }
  static_modint &operator/=(const static_modint &that) {
    return *this *= static_modint(std::get<1>(extgcd(that.data, int(MOD))));
  }
  static_modint &operator%=(const static_modint &that) {
    data %= that.data;
    return *this;
  }
  static_modint &operator+=(const int that) {
    return *this += static_modint(that);
  }
  static_modint &operator-=(const int that) {
    return *this -= static_modint(that);
  }
  static_modint &operator*=(const int that) {
    return *this *= static_modint(that);
  }
  static_modint &operator/=(const int that) {
    return *this /= static_modint(that);
  }
  static_modint &operator%=(const int that) {
    return *this %= static_modint(that);
  }
  bool operator<(const static_modint &that) { return data < that.data; }
  bool operator>(const static_modint &that) { return data > that.data; }
  bool operator<=(const static_modint &that) { return data <= that.data; }
  bool operator>=(const static_modint &that) { return data >= that.data; }
  bool operator!=(const static_modint &that) { return data != that.data; }
  bool operator==(const static_modint &that) { return data == that.data; }
  unsigned val() const { return data; }
  unsigned mod() const { return MOD; }
  static_modint operator+(const static_modint &that) const {
    return static_modint(*this) += that;
  }
  static_modint operator-(const static_modint &that) const {
    return static_modint(*this) -= that;
  }
  static_modint operator*(const static_modint &that) const {
    return static_modint(*this) *= that;
  }
  static_modint operator/(const static_modint &that) const {
    return static_modint(*this) /= that;
  }
  static_modint operator%(const static_modint &that) const {
    return static_modint(*this) %= that;
  }
  static_modint operator+(const int that) const {
    return static_modint(*this) += that;
  }
  static_modint operator-(const int that) const {
    return static_modint(*this) -= that;
  }
  static_modint operator*(const int that) const {
    return static_modint(*this) *= that;
  }
  static_modint operator/(const int that) const {
    return static_modint(*this) /= that;
  }
  static_modint operator%(const int that) const {
    return static_modint(*this) %= that;
  }
  static_modint operator=(const int that) {
    return *this = static_modint(that);
  }
  friend std::istream &operator>>(std::istream &is, static_modint &that) {
    int64_t tmp;
    is >> tmp;
    that = static_modint(tmp);
    return is;
  }
  friend std::ostream &operator<<(std::ostream &os, const static_modint &that) {
    return os << that.data;
  }
  static_modint power(ll n) const {
    static_modint b = raw(1), a = *this;
    while (n) {
      if (n & 1) {
        b *= a;
      }
      a *= a;
      n >>= 1;
    }
    return b;
  }

 private:
  unsigned data;
  inline std::tuple<int32_t, int32_t, int32_t> extgcd(int32_t a,
                                                      int32_t b) const {
    if (b == 0) {
      return std::make_tuple(a, 1, 0);
    }
    const auto ret = extgcd(b, a % b);
    std::swap(std::get<1>(ret), std::get<2>(ret));
    std::get<2>(ret) -= a / b * std::get<1>(ret);
    return ret;
  }
};
using modint998244353 = static_modint<998244353>;
using mint = modint998244353;
int main(void) {
  int n;
  cin >> n;
  vector<int> x(n + 1), y(n + 1), s(n + 1);
  vector<mint> sum(n + 1);
  for (int i = int(1); i < int(n + 1); ++i) {
    cin >> x[i] >> y[i] >> s[i];
  }
  mint ans = x[n] + 1;
  for (int i = int(1); i < int(n + 1); ++i) {
    const int j = lower_bound(begin(x), end(x), y[i]) - begin(x);
    const mint cur = mint::raw(x[i] - y[i]) + sum[i - 1] - sum[j - 1];
    sum[i] = sum[i - 1] + cur;
    if (s[i]) {
      ans += cur;
    }
  }
  cout << ans << endl;
  return 0;
}

```