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: /**
   * check out my youtube channel sh0rkyboy
   * https://tinyurl.com/zdxe2y4z
   * I do screencasts, solutions, and other fun stuff in the future
   */
  
  import java.util.*;
  import java.io.*;
  import static java.lang.Math.min;
  import static java.lang.Math.abs;
  import static java.lang.Math.max;
  public class EdA {
  	static long[] mods = {1000000007, 998244353, 1000000009};
  	static long mod = mods[1];
  	public static MyScanner sc;
      public static PrintWriter out;
  	public static void main(String[] largewang) throws Exception{
  		// TODO Auto-generated method stub
   		sc = new MyScanner();
   		out = new PrintWriter(System.out);
   		int n = sc.nextInt();
   		int[][] portals = new int[n][3];
   		for(int j = 0;j<n;j++){
   			for(int k = 0;k<3;k++){
   				portals[j][k] = sc.nextInt();
   			}
   		}
   		long[] dp = new long[n];// amount of time it takes to get back to itself if its 1
   		dp[0] = portals[0][0] - portals[0][1];
   		LazySegmentTree ls = new LazySegmentTree(dp);
   		for(int j = 1;j<n;j++){
   			int l = 0;
   			int r = n;
   			while(l < r) {
   				int mid = (l+r)/2;
   				if (portals[mid][0] < portals[j][1]) {
   					l = mid+1;
   				} else {
   					r = mid;
   				}
   			}
   			long sum = ls.querySum(l,  j-1);
   			sum += (portals[j][0] - portals[j][1]);
   			sum %= mod;
   			ls.update(j, j,  sum);
   		}
   		
   		long ans = (portals[n-1][0] + 1)%mod;
   		for(int j = 0;j<n;j++){
   			if (portals[j][2] == 1) {
   				ans += ls.queryMax(j, j);
   				ans %= mod;
   			}
   		}
   		out.println(ans);
   		out.close();
   		
   	}
  	static class LazySegmentTree {
  		int size;
  		private long[] max;
  		private long[] min;
  		private long[] sum;
  		private long[] lazy;
  		public LazySegmentTree(long[] array){
  			size = array.length;
  			max=new long[array.length*4];
  			min=new long[array.length*4];
  			sum=new long[array.length*4];
  			lazy=new long[array.length*4];
  			buildTree(array,1,0,size-1);
  		}
  
  		private void buildTree(long[] array, int position, int front, int end){
  			//Basis case for returning the elements itself
  			if(front==end){
  				max[position]=array[front];
  				min[position]=array[front];
  				sum[position]=array[front];
  				return;
  			}
  			//Building trees by dividing into two subtrees
  			buildTree(array, position*2,front,(front+end)/2);
  			buildTree(array, position*2+1,(front+end)/2+1,end);
  
  			recalc(position,front,end);
  		}
  
  		private void recalc(int position, int front, int end) {
  			max[position]=Math.max(getMax(position*2),getMax(position*2+1) );
  			min[position]=Math.min(getMin(position*2), getMin(position*2+1));
  			sum[position]=getSum(position*2,front,(front+end)/2)+getSum(position*2+1,(front+end)/2+1,end);
  		}
  
  		private long getMax(int position){
  			return (max[position]+lazy[position])%mod;
  		}
  
  		private long getMin(int position){
  			return (min[position]+lazy[position])%mod;
  		}
  
  		private long getSum(int position,int front,int end){
  			return (sum[position]+lazy[position]*(long)(end-front+1))%mod;
  		}
  
  		public long queryMax(int queryFront, int queryEnd) {
  			return queryMax(1,0,size-1,queryFront,queryEnd);
  		}
  
  		private long queryMax(int position,int front,int end, int queryFront, int queryEnd){
  			//Case for entirely inclusive
  			if(front>=queryFront&&queryEnd>=end){
  				return getMax(position);
  			}
  			//Case for entirely exclusive
  			if(end<queryFront||queryEnd<front){
  				return Long.MIN_VALUE;
  			}
  			propagate(position,front,end);
  			long leftAns=queryMax(position*2,front,(front+end)/2,queryFront,queryEnd);
  			long rightAns=queryMax(position*2+1,(front+end)/2+1,end,queryFront,queryEnd);
  			return Math.max(leftAns, rightAns);
  		}
  
  		public long queryMin(int queryFront, int queryEnd) {
  			return queryMin(1,0,size-1,queryFront,queryEnd);
  		}
  
  		private long queryMin(int position,int front,int end, int queryFront, int queryEnd){
  			//Case for entirely inclusive
  			if(front>=queryFront&&queryEnd>=end){
  				return getMin(position);
  			}
  			//Case for entirely exclusive
  			if(end<queryFront||queryEnd<front){
  				return Long.MAX_VALUE;
  			}
  			propagate(position,front,end);
  			long leftAns=queryMin(position*2,front,(front+end)/2,queryFront,queryEnd);
  			long rightAns=queryMin(position*2+1,(front+end)/2+1,end,queryFront,queryEnd);
  			return Math.min(leftAns, rightAns);
  		}
  		public long querySum(int queryFront, int queryEnd) {
  			return querySum(1,0,size-1,queryFront,queryEnd);
  		}
  
  		private long querySum(int position,int front,int end, int queryFront, int queryEnd){
  			//Case for entirely inclusive
  			if(front>=queryFront&&queryEnd>=end){
  				return getSum(position,front,end);
  			}
  			//Case for entirely exclusive
  			if(end<queryFront||queryEnd<front){
  				return 0;
  			}
  			propagate(position,front,end);
  			long leftAns=querySum(position*2,front,(front+end)/2,queryFront,queryEnd);
  			long rightAns=querySum(position*2+1,(front+end)/2+1,end,queryFront,queryEnd);
  			return (leftAns+rightAns)%mod;
  		}
  
  
  		private void propagate(int position,int front, int end){
  			lazy[position*2]+=lazy[position];
  			lazy[position*2]%=mod;
  			lazy[position*2+1]+=lazy[position];
  			lazy[position*2+1]%=mod;
  			max[position]=getMax(position);
  			min[position]=getMin(position);
  			sum[position]=getSum(position,front,end);
  			lazy[position]=0;
  			//Set the propagation to 0 so that it will not propagate the value again and again
  		}
  
  		public void update(int updateFront, int updateEnd, long value){
  			update(1,0,size-1,updateFront,updateEnd,value);
  		}
  
  		private void update(int position, int front, int end, int updateFront,int updateEnd,long value){
  			//Case for entirely inclusive
  			if(front>=updateFront&&updateEnd>=end){
  				lazy[position]+=value;
  				lazy[position]%=mod;
  				return;
  			}
  			//Case for entirely exclusive
  			if(end<updateFront||updateEnd<front){
  				return;
  			}
  			propagate(position,front,end);
  			update(position*2,front,(front+end)/2,updateFront,updateEnd,value);
  			update(position*2+1,(front+end)/2+1,end,updateFront,updateEnd,value);
  			recalc(position,front,end);
  		}
  	}
  	public static void sort(int[] array){
  		ArrayList<Integer> copy = new ArrayList<>();
  		for (int i : array)
  			copy.add(i);
  		Collections.sort(copy);
  		for(int i = 0;i<array.length;i++)
  			array[i] = copy.get(i);
  	}
  	static String[] readArrayString(int n){
  		String[] array = new String[n];
  		for(int j =0 ;j<n;j++)
  			array[j] = sc.next();
  		return array;
  	}
  	static int[] readArrayInt(int n){
      	int[] array = new int[n];
      	for(int j = 0;j<n;j++)
      		array[j] = sc.nextInt();
      	return array;
      }
  	static int[] readArrayInt1(int n){
  		int[] array = new int[n+1];
  		for(int j = 1;j<=n;j++){
  			array[j] = sc.nextInt();
  		}
  		return array;
  	}
  	static long[] readArrayLong(int n){
  		long[] array = new long[n];
  		for(int j =0 ;j<n;j++)
  			array[j] = sc.nextLong();
  		return array;
  	}
  	static double[] readArrayDouble(int n){
  		double[] array = new double[n];
  		for(int j =0 ;j<n;j++)
  			array[j] = sc.nextDouble();
  		return array;
  	}
  	static int minIndex(int[] array){
  		int minValue = Integer.MAX_VALUE;
  		int minIndex = -1;
  		for(int j = 0;j<array.length;j++){
  			if (array[j] < minValue){
  				minValue = array[j];
  				minIndex = j;
  			}
  		}
  		return minIndex;
  	}
  	static int minIndex(long[] array){
  		long minValue = Long.MAX_VALUE;
  		int minIndex = -1;
  		for(int j = 0;j<array.length;j++){
  			if (array[j] < minValue){
  				minValue = array[j];
  				minIndex = j;
  			}
  		}
  		return minIndex;
  	}
  	static int minIndex(double[] array){
  		double minValue = Double.MAX_VALUE;
  		int minIndex = -1;
  		for(int j = 0;j<array.length;j++){
  			if (array[j] < minValue){
  				minValue = array[j];
  				minIndex = j;
  			}
  		}
  		return minIndex;
  	}
  	static long power(long x, long y){
  		if (y == 0)
  			return 1;
  		if (y%2 == 1)
  			return (x*power(x, y-1))%mod;
  		return power((x*x)%mod, y/2)%mod;
  	}
  	static void verdict(boolean a){
          out.println(a ? "YES" : "NO");
      }
      public static class MyScanner {
          BufferedReader br;
          StringTokenizer st;
          public MyScanner() {
              br = new BufferedReader(new InputStreamReader(System.in));
          }
          String next() {
              while (st == null || !st.hasMoreElements()) {
                  try {
                      st = new StringTokenizer(br.readLine());
                  } 
                  catch (IOException e) {
                      e.printStackTrace();
                  }
              }
              return st.nextToken();
          }
          int nextInt() {
              return Integer.parseInt(next());
          }
          long nextLong() {
              return Long.parseLong(next());
          }
          double nextDouble() {
              return Double.parseDouble(next());
          }
          String nextLine() {
              String str = "";
              try{
                  str = br.readLine();
              } 
              catch (IOException e) {
                  e.printStackTrace();
              }
              return str;
          }
          
      }	
  }
  
  //StringJoiner sj = new StringJoiner(" "); 
  //sj.add(strings)
  //sj.toString() gives string of those stuff w spaces or whatever that sequence is