description: 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.
solution: #include <bits/stdc++.h>
  using namespace std;
  using LL = long long;
  template <typename T>
  T inverse(T a, T m) {
    T u = 0, v = 1;
    while (a != 0) {
      T t = m / a;
      m -= t * a;
      swap(a, m);
      u -= t * v;
      swap(u, v);
    }
    assert(m == 1);
    return u;
  }
  template <typename T>
  class Modular {
   public:
    using Type = typename decay<decltype(T::value)>::type;
    constexpr Modular() : value() {}
    template <typename U>
    Modular(const U& x) {
      value = normalize(x);
    }
    template <typename U>
    static Type normalize(const U& x) {
      Type v;
      if (-mod() <= x && x < mod())
        v = static_cast<Type>(x);
      else
        v = static_cast<Type>(x % mod());
      if (v < 0) v += mod();
      return v;
    }
    const Type& operator()() const { return value; }
    template <typename U>
    explicit operator U() const {
      return static_cast<U>(value);
    }
    constexpr static Type mod() { return T::value; }
    Modular& operator+=(const Modular& other) {
      if ((value += other.value) >= mod()) value -= mod();
      return *this;
    }
    Modular& operator-=(const Modular& other) {
      if ((value -= other.value) < 0) value += mod();
      return *this;
    }
    template <typename U>
    Modular& operator+=(const U& other) {
      return *this += Modular(other);
    }
    template <typename U>
    Modular& operator-=(const U& other) {
      return *this -= Modular(other);
    }
    Modular& operator++() { return *this += 1; }
    Modular& operator--() { return *this -= 1; }
    Modular operator++(int) {
      Modular result(*this);
      *this += 1;
      return result;
    }
    Modular operator--(int) {
      Modular result(*this);
      *this -= 1;
      return result;
    }
    Modular operator-() const { return Modular(-value); }
    template <typename U = T>
    typename enable_if<is_same<typename Modular<U>::Type, int>::value,
                       Modular>::type&
    operator*=(const Modular& rhs) {
      value = normalize(static_cast<int64_t>(value) *
                        static_cast<int64_t>(rhs.value));
      return *this;
    }
    template <typename U = T>
    typename enable_if<is_same<typename Modular<U>::Type, long long>::value,
                       Modular>::type&
    operator*=(const Modular& rhs) {
      long long q = static_cast<long long>(static_cast<long double>(value) *
                                           rhs.value / mod());
      value = normalize(value * rhs.value - q * mod());
      return *this;
    }
    template <typename U = T>
    typename enable_if<!is_integral<typename Modular<U>::Type>::value,
                       Modular>::type&
    operator*=(const Modular& rhs) {
      value = normalize(value * rhs.value);
      return *this;
    }
    Modular& operator/=(const Modular& other) {
      return *this *= Modular(inverse(other.value, mod()));
    }
    friend const Type& abs(const Modular& x) { return x.value; }
    template <typename U>
    friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);
    template <typename U>
    friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);
    template <typename V, typename U>
    friend V& operator>>(V& stream, Modular<U>& number);
  
   private:
    Type value;
  };
  template <typename T>
  bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) {
    return lhs.value == rhs.value;
  }
  template <typename T, typename U>
  bool operator==(const Modular<T>& lhs, U rhs) {
    return lhs == Modular<T>(rhs);
  }
  template <typename T, typename U>
  bool operator==(U lhs, const Modular<T>& rhs) {
    return Modular<T>(lhs) == rhs;
  }
  template <typename T>
  bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) {
    return !(lhs == rhs);
  }
  template <typename T, typename U>
  bool operator!=(const Modular<T>& lhs, U rhs) {
    return !(lhs == rhs);
  }
  template <typename T, typename U>
  bool operator!=(U lhs, const Modular<T>& rhs) {
    return !(lhs == rhs);
  }
  template <typename T>
  bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) {
    return lhs.value < rhs.value;
  }
  template <typename T>
  Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) {
    return Modular<T>(lhs) += rhs;
  }
  template <typename T, typename U>
  Modular<T> operator+(const Modular<T>& lhs, U rhs) {
    return Modular<T>(lhs) += rhs;
  }
  template <typename T, typename U>
  Modular<T> operator+(U lhs, const Modular<T>& rhs) {
    return Modular<T>(lhs) += rhs;
  }
  template <typename T>
  Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) {
    return Modular<T>(lhs) -= rhs;
  }
  template <typename T, typename U>
  Modular<T> operator-(const Modular<T>& lhs, U rhs) {
    return Modular<T>(lhs) -= rhs;
  }
  template <typename T, typename U>
  Modular<T> operator-(U lhs, const Modular<T>& rhs) {
    return Modular<T>(lhs) -= rhs;
  }
  template <typename T>
  Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) {
    return Modular<T>(lhs) *= rhs;
  }
  template <typename T, typename U>
  Modular<T> operator*(const Modular<T>& lhs, U rhs) {
    return Modular<T>(lhs) *= rhs;
  }
  template <typename T, typename U>
  Modular<T> operator*(U lhs, const Modular<T>& rhs) {
    return Modular<T>(lhs) *= rhs;
  }
  template <typename T>
  Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) {
    return Modular<T>(lhs) /= rhs;
  }
  template <typename T, typename U>
  Modular<T> operator/(const Modular<T>& lhs, U rhs) {
    return Modular<T>(lhs) /= rhs;
  }
  template <typename T, typename U>
  Modular<T> operator/(U lhs, const Modular<T>& rhs) {
    return Modular<T>(lhs) /= rhs;
  }
  template <typename T, typename U>
  Modular<T> power(const Modular<T>& a, const U& b) {
    assert(b >= 0);
    Modular<T> x = a, res = 1;
    U p = b;
    while (p > 0) {
      if (p & 1) res *= x;
      x *= x;
      p >>= 1;
    }
    return res;
  }
  template <typename T>
  bool IsZero(const Modular<T>& number) {
    return number() == 0;
  }
  template <typename T>
  string to_string(const Modular<T>& number) {
    return to_string(number());
  }
  template <typename U, typename T>
  U& operator<<(U& stream, const Modular<T>& number) {
    return stream << number();
  }
  template <typename U, typename T>
  U& operator>>(U& stream, Modular<T>& number) {
    typename common_type<typename Modular<T>::Type, long long>::type x;
    stream >> x;
    number.value = Modular<T>::normalize(x);
    return stream;
  }
  using ModType = int;
  struct VarMod {
    static ModType value;
  };
  ModType VarMod::value;
  ModType& md = VarMod::value;
  using Mint = Modular<VarMod>;
  const int MAXN = 500010;
  struct Node {
    int x, y, s;
  };
  Node x[MAXN];
  int add[MAXN];
  int dist[MAXN];
  int on[MAXN];
  Mint diff[MAXN];
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    md = 998244353;
    int n;
    cin >> n;
    map<int, int> h;
    for (int i = 0; i < n; ++i) {
      cin >> x[i].x >> x[i].y >> x[i].s;
      h[x[i].x] = h[x[i].y] = 0;
    }
    int nn = 0;
    for (auto& [a, b] : h) b = ++nn;
    for (int i = 0; i < n; ++i) {
      add[h[x[i].x]] = h[x[i].y];
      add[h[x[i].y]] = -1;
      on[h[x[i].x]] = x[i].s;
      dist[h[x[i].x]] = x[i].x - x[i].y;
    }
    Mint ans = h.rbegin()->first + 1;
    Mint now = 0;
    for (int i = nn; i >= 1; --i) {
      if (add[i] == -1) {
        now += diff[i];
        continue;
      }
      ans += Mint(dist[i]) * (now + on[i]);
      diff[add[i]] = -now - on[i];
      now += now + on[i];
    }
    cout << ans << endl;
    return 0;
  }
  