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: 					/*
  					TO LEARN
  					1-segment trees
  					2-euler tour
  				
  					*/
  					/*
  					TO SOLVE 
  					uva 1103
  					codeforces 722 kavi on pairing duty
  					*/
  					/*
  					bit manipulation shit
  					1-Computer Systems: A Programmer's Perspective
  					2-hacker's delight
  					3-(02-03-bits-ints)
  					4-machine-basics
  					5-Bits Manipulation tutorialspoint
  					*/
  					/*
  					TO WATCH
  					1-what is bitmasking by kartik arora youtube
  					*/
  					            import java.util.*;
  							    import java.math.*;
  							    import java.io.*; 
  							    import java.util.stream.Collectors;
  
  							       public class A{
  					            static FastScanner scan=new FastScanner();
  							     public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
  	static Pair arr[];
  	static int n;
  	static int bin(int to)
  	{
  		int l=0,r=n-1;
  		int ret=-1;
  		while(l<=r)
  		{
  			int mid=(l+r)/2;
  			//System.out.println(to);
  			if(arr[mid].x>arr[to].y)
  			{
  				ret=mid;
  				r=mid-1;
  			}
  			else 
  			{
  				l=mid+1;
  			}
  		}
  		return ret;
  
  	}
  		static int bin2(int l,int r,int to)
  	{
  		
  		int ret=-1;
  		while(l<=r)
  		{
  			int mid=(l+r)/2;
  			//System.out.println(to);
  		//System.out.println(mid+" "+l+" "+r+" "+to);
  			if(arr[mid].x>arr[to].y)
  			{
  
  				ret=mid;
  				l=mid+1;
  			}
  			else 
  			{
  				l=mid+1;
  			}
  		}
  		return ret;
  
  	}
  	static final long mod=998244353L;
  				public static void main(String[] args)  throws Exception 
  					      {
  					 
  
  								///scan=new FastScanner("cownomics.in");
  							//out = new PrintWriter("cownomics.out");
  							 
  							 							 /*
  							 READING 
  						
  							 3-Introduction to DP with Bitmasking codefoces
  							 4-Bit Manipulation hackerearth
  							 5-read more about mobious and inculsion-exclusion 
  							 */
  		
  
  							    int tt=1;	  
  				//tt=scan.nextInt();
  						    
  						//	System.out.println(2^3);
  						 outer:while(tt-->0)
  							    {
  							   
  							 
  							    n=scan.nextInt();
  							    arr=new Pair[n];
  							  
  							   boolean active[]=new boolean[20];
  							   for(int i=0;i<n;i++){
  							   	arr[i]=new Pair(scan.nextLong(),scan.nextLong(),scan.nextInt());
  							 
  }
  ArrayList<Long>RESs=new ArrayList<Long>();
  //RESs.add(0);
  long pre[]=new long[n];
  for(int i=0;i<n;i++)
  {
  	int idx=bin(i);
  	int idx2=bin2(0,i-1,i);
  	if(idx==-1||idx>=i)
  	{
  		long tmp=((arr[i].x-arr[i].y));
  	tmp=(tmp+mod)%mod;
  		RESs.add(tmp);
  		if(i==0)
  		pre[i]=tmp;
  	else pre[i]=tmp+pre[i-1];
  	pre[i]=(pre[i]+mod)%mod;
  	
  		continue;
  	}
  	else 
  	{
  		long tmp=((arr[i].x-arr[i].y));
  		tmp=(tmp+mod)%mod;
  	//	if(tmp>=mod)
  		//	tmp-=mod;
  		
  		if(idx==0)
  		{
  			tmp+=pre[idx2];
  			tmp=(tmp+mod)%mod;
  		}
  		else 
  		{
  			tmp+=((pre[idx2]-pre[idx-1]));
  			tmp=(tmp+mod)%mod;
  		   
  		}
  		RESs.add(tmp);
  		pre[i]=pre[i-1]+tmp;
  		pre[i]=(pre[i]+mod)%mod;
  	}
  }
  long res=0;
  //out.println(RESs);
  //out.println(Arrays.toString(pre));
  for(int i=0;i<n;i++)
  if(arr[i].z==1){
  	res+=RESs.get(i);
  res=(res+mod)%mod;
  }
  out.println((res+arr[n-1].x+1)%mod);
  
  
  
  							   
  							    	
  							    	}
  
  							
  
  	out.close();
  				}
  	    
  							static class special{
  								boolean bool;
  								long n;
  								//int id;
  								special(boolean bool,long n)
  								{
  									this.bool=bool;
  									this.n=n;
  								}
  								@Override
  								    public int hashCode() {
  							                int hash = 7;
  				    hash = 31 * hash + (int) n;
  				   
  				    return hash;
  							            }
  							              @Override
  							            public boolean equals(Object o){
  							                if (o == this) return true;
  							                if (o.getClass() != getClass()) return false;
  							                special t = (special)o;
  							                return t.bool == bool && t.n == n;
  							            }
  							}
  
  							   
  							     static long binexp(long a,long n)
  							    {
  							        if(n==0)
  							            return 1;
  							        long res=binexp(a,n/2);
  							        if(n%2==1)
  							            return res*res*a;
  							        else 
  							            return res*res;
  							    }
  							     
  							    static long powMod(long base, long exp, long mod) {
  							           if (base == 0 || base == 1) return base;
  							           if (exp == 0) return 1;
  							           if (exp == 1) return (base % mod+mod)%mod;
  							           long R = (powMod(base, exp/2, mod) % mod+mod)%mod;
  							           R *= R;
  							           R %= mod;
  							           if ((exp & 1) == 1) {
  							               return (base * R % mod+mod)%mod;
  							           }
  							           else return (R %mod+mod)%mod;
  							       }
  							    static double dis(double x1,double y1,double x2,double y2)
  							    {
  							        return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
  							    }
  							    static long mod(long x,long y)
  							        {
  							            if(x<0)
  							                x=x+(-x/y+1)*y;
  							            return x%y;
  							        }
  							     public static  long pow(long b, long e) {
  							                long r = 1;
  							                while (e > 0) {
  							                    if (e % 2 == 1) r = r * b ;
  							                    b = b * b;
  							                    e >>= 1;
  							                }
  							                return r;
  							            }
  							    private static void sort(long[] arr) {
  							            List<Long> list = new ArrayList<>();
  							            for (long object : arr) list.add(object);
  							            Collections.sort(list);
  							        //Collections.reverse(list);
  							            for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
  							        }
  							    private static void sort2(long[] arr) {
  							            List<Long> list = new ArrayList<>();
  							            for (Long object : arr) list.add(object);
  							            Collections.sort(list);
  							        Collections.reverse(list);
  							            for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
  							        }
  							     static class FastScanner
  					{
  					    private int BS = 1 << 16;
  					    private char NC = (char) 0;
  					    private byte[] buf = new byte[BS];
  					    private int bId = 0, size = 0;
  					    private char c = NC;
  					    private double cnt = 1;
  					    private BufferedInputStream in;
  					 
  					    public FastScanner() {
  					        in = new BufferedInputStream(System.in, BS);
  					    }
  					 
  					    public FastScanner(String s) {
  					        try {
  					            in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
  					        } catch (Exception e) {
  					            in = new BufferedInputStream(System.in, BS);
  					        }
  					    }
  					 
  					    private char getChar() {
  					        while (bId == size) {
  					            try {
  					                size = in.read(buf);
  					            } catch (Exception e) {
  					                return NC;
  					            }
  					            if (size == -1) return NC;
  					            bId = 0;
  					        }
  					        return (char) buf[bId++];
  					    }
  					 
  					    public int nextInt() {
  					        return (int) nextLong();
  					    }
  					 
  					    public int[] nextInts(int N) {
  					        int[] res = new int[N];
  					        for (int i = 0; i < N; i++) {
  					            res[i] = (int) nextLong();
  					        }
  					        return res;
  					    }
  					 
  					    public long[] nextLongs(int N) {
  					        long[] res = new long[N];
  					        for (int i = 0; i < N; i++) {
  					            res[i] = nextLong();
  					        }
  					        return res;
  					    }
  					 
  					    public long nextLong() {
  					        cnt = 1;
  					        boolean neg = false;
  					        if (c == NC) c = getChar();
  					        for (; (c < '0' || c > '9'); c = getChar()) {
  					            if (c == '-') neg = true;
  					        }
  					        long res = 0;
  					        for (; c >= '0' && c <= '9'; c = getChar()) {
  					            res = (res << 3) + (res << 1) + c - '0';
  					            cnt *= 10;
  					        }
  					        return neg ? -res : res;
  					    }
  					 
  					    public double nextDouble() {
  					        double cur = nextLong();
  					        return c != '.' ? cur : cur + nextLong() / cnt;
  					    }
  					 
  					    public double[] nextDoubles(int N) {
  					        double[] res = new double[N];
  					        for (int i = 0; i < N; i++) {
  					            res[i] = nextDouble();
  					        }
  					        return res;
  					    }
  					 
  					    public String next() {
  					        StringBuilder res = new StringBuilder();
  					        while (c <= 32) c = getChar();
  					        while (c > 32) {
  					            res.append(c);
  					            c = getChar();
  					        }
  					        return res.toString();
  					    }
  					 
  					    public String nextLine() {
  					        StringBuilder res = new StringBuilder();
  					        while (c <= 32) c = getChar();
  					        while (c != '\n') {
  					            res.append(c);
  					            c = getChar();
  					        }
  					        return res.toString();
  					    }
  					 
  					    public boolean hasNext() {
  					        if (c > 32) return true;
  					        while (true) {
  					            c = getChar();
  					            if (c == NC) return false;
  					            else if (c > 32) return true;
  					        }
  					    }
  					}
  
  							     static class Pair implements Comparable<Pair>{
  							            public long x, y,z;
  							            public Pair(long x1, long y1,long z1) {
  							                x=x1;
  							                y=y1;
  							                z=z1;
  							            }
  							             public Pair(long x1, long y1) {
  							                x=x1;
  							                y=y1;
  							    
  							            }
  							            
  							            @Override
  							            public int hashCode() {
  							                return (int)(x + 31 * y);
  							            }
  							            public String toString() {
  							                return x + " " + y+" "+z;
  							            }
  							            @Override
  							            public boolean equals(Object o){
  							                if (o == this) return true;
  							                if (o.getClass() != getClass()) return false;
  							                Pair t = (Pair)o;
  							                return t.x == x && t.y == y&&t.z==z;
  							            }
  							    public int compareTo(Pair o)
  							    {
  							    	
  							   
  							    	return (int)(z-o.z);
  							     
  							        }
  							     
  							    }
  							    
  							    }
  
  							     
  