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.
import java.util.TreeMap;

public class Main {

	public static void main(String[] args) {
		new Main();
	}

	public Main() {
		FastScanner fs = new FastScanner();
		java.io.PrintWriter out = new java.io.PrintWriter(System.out);
		solve(fs, out);
		out.flush();
	}

	public void solve(FastScanner fs, java.io.PrintWriter out) {
		int n = fs.nextInt();
		SegTree seg = new SegTree(n);
		int ans = 0;
		TreeMap<Integer, Integer> map = new TreeMap<>();
		for (int i = 0;i < n;++ i) {
			int x = fs.nextInt(), y = fs.nextInt(), s = fs.nextInt();
			map.put(x, i);
			int warp = map.ceilingEntry(y).getValue();
			int d = plus(seg.sum(warp, i), x - y);
			seg.add(i, d);
			if (s == 1) ans = plus(ans, d);
			if (i == n - 1) ans = plus(ans, x + 1);
		}
		out.println(ans);
	}

	class SegTree {
		int[] array;
		int len;
		SegTree(int n) {
			len = Integer.highestOneBit(n - 1) << 1;
			array = new int[len << 1];
		}
		void add(int i, int n) {
			i += len;
			while(i > 0) {
				array[i] = plus(array[i], n);
				i >>= 1;
			}
		}
		int sum(int l, int r) {
			l += len;
			r += len;
			int ans = 0;
			while(l < r) {
				if (l % 2 != 0) ans = plus(ans, array[l++]);
				if (r % 2 != 0) ans = plus(ans, array[--r]);
				l >>= 1;
				r >>= 1;
			}
			return ans;
		}
	}

	final int MOD = 998_244_353;
	int plus(int n, int m) {
		int sum = n + m;
		if (sum >= MOD) sum -= MOD;
		return sum;
	}
	int minus(int n, int m) {
		int sum = n - m;
		if (sum < 0) sum += MOD;
		return sum;
	}
	int times(int n, int m) {
		return (int)((long)n * m % MOD);
	}
	int divide(int n, int m) {
		return times(n, IntMath.pow(m, MOD - 2, MOD));
	}
	int[] fact, invf;
	void calc(int len) {
		len += 2;
		fact = new int[len];
		invf = new int[len];
		fact[0] = fact[1] = invf[0] = invf[1] = 1;
		for (int i = 2;i < fact.length;++ i) fact[i] = times(fact[i - 1], i);
		invf[len - 1] = divide(1, fact[len - 1]);
		for (int i = len - 1;i > 1;-- i) invf[i - 1] = times(i, invf[i]);
	}
	int comb(int n, int m) {
		if (n < m) return 0;
		return times(fact[n], times(invf[n - m], invf[m]));
	}
}

class FastScanner {

	private final java.io.InputStream in = System.in;
	private final byte[] buffer = new byte[8192];
	private int ptr = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (ptr < buflen) return true;
		ptr = 0;
		try {
			buflen = in.read(buffer);
		} catch (java.io.IOException e) {
			e.printStackTrace();
		}
		return buflen > 0;
	}

	private byte readByte() {
		return hasNextByte() ? buffer[ptr++ ] : -1;
	}

	private static boolean isPrintableChar(byte c) {
		return 32 < c || c < 0;
	}

	private static boolean isNumber(int c) {
		return '0' <= c && c <= '9';
	}

	public boolean hasNext() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr]))
			ptr++ ;
		return hasNextByte();
	}

	public String next() {
		if (!hasNext()) throw new java.util.NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		byte b;
		while (isPrintableChar(b = readByte()))
			sb.appendCodePoint(b);
		return sb.toString();
	}

	public final char nextChar() {
		if (!hasNext()) throw new java.util.NoSuchElementException();
		return (char)readByte();
	}

	public final long nextLong() {
		if (!hasNext()) throw new java.util.NoSuchElementException();
		long n = 0;
		try {
			byte b = readByte();
			if (b == '-') {
				while (isNumber(b = readByte()))
					n = n * 10 + '0' - b;
				return n;
			} else if (!isNumber(b)) throw new NumberFormatException();
			do
				n = n * 10 + b - '0';
			while (isNumber(b = readByte()));
		} catch (java.util.NoSuchElementException e) {}
		return n;
	}

	public final int nextInt() {
		if (!hasNext()) throw new java.util.NoSuchElementException();
		int n = 0;
		try {
			byte b = readByte();
			if (b == '-') {
				while (isNumber(b = readByte()))
					n = n * 10 + '0' - b;
				return n;
			} else if (!isNumber(b)) throw new NumberFormatException();
			do
				n = n * 10 + b - '0';
			while (isNumber(b = readByte()));
		} catch (java.util.NoSuchElementException e) {}
		return n;
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}

class Arrays {

	public static void sort(final int[] array) {
		sort(array, 0, array.length);
	}

	public static void sort(final int[] array, int fromIndex, int toIndex) {
		if (toIndex - fromIndex  <= 512) {
			java.util.Arrays.sort(array, fromIndex, toIndex);
			return;
		}
		sort(array, fromIndex, toIndex, 0, new int[array.length]);
	}

	private static final void sort(int[] a, final int from, final int to, final int l, final int[] bucket) {
		if (to - from  <= 512) {
			java.util.Arrays.sort(a, from, to);
			return;
		}
		final int BUCKET_SIZE = 256;
		final int INT_RECURSION = 4;
		final int MASK = 0xff;
		final int shift = l << 3;
		final int[] cnt = new int[BUCKET_SIZE + 1];
		final int[] put = new int[BUCKET_SIZE];
		for (int i = from; i < to; i++) ++ cnt[(a[i] >>> shift & MASK) + 1];
		for (int i = 0; i < BUCKET_SIZE; i++) cnt[i + 1] += cnt[i];
		for (int i = from; i < to; i++) {
			int bi = a[i] >>> shift & MASK;
			bucket[cnt[bi] + put[bi]++] = a[i];
		}
		for (int i = BUCKET_SIZE - 1, idx = from; i >= 0; i--) {
			int begin = cnt[i];
			int len = cnt[i + 1] - begin;
			System.arraycopy(bucket, begin, a, idx, len);
			idx += len;
	    }
		final int nxtL = l + 1;
		if (nxtL < INT_RECURSION) {
			sort(a, from, to, nxtL, bucket);
			if (l == 0) {
				int lft, rgt;
				lft = from - 1; rgt = to;
				while (rgt - lft > 1) {
					int mid = lft + rgt >> 1;
					if (a[mid] < 0) lft = mid;
					else rgt = mid;
				}
				reverse(a, from, rgt);
				reverse(a, rgt, to);
	        }
	    }
	}

	public static void sort(final long[] array) {
		sort(array, 0, array.length);
	}

	public static void sort(final long[] array, int fromIndex, int toIndex) {
		if (toIndex - fromIndex  <= 512) {
			java.util.Arrays.sort(array, fromIndex, toIndex);
			return;
		}
		sort(array, fromIndex, toIndex, 0, new long[array.length]);
	}

	private static final void sort(long[] a, final int from, final int to, final int l, final long[] bucket) {
		final int BUCKET_SIZE = 256;
		final int LONG_RECURSION = 8;
		final int MASK = 0xff;
		final int shift = l << 3;
		final int[] cnt = new int[BUCKET_SIZE + 1];
		final int[] put = new int[BUCKET_SIZE];
		for (int i = from; i < to; i++) ++ cnt[(int) ((a[i] >>> shift & MASK) + 1)];
		for (int i = 0; i < BUCKET_SIZE; i++) cnt[i + 1] += cnt[i];
		for (int i = from; i < to; i++) {
			int bi = (int) (a[i] >>> shift & MASK);
			bucket[cnt[bi] + put[bi]++] = a[i];
		}
		for (int i = BUCKET_SIZE - 1, idx = from; i >= 0; i--) {
			int begin = cnt[i];
			int len = cnt[i + 1] - begin;
			System.arraycopy(bucket, begin, a, idx, len);
			idx += len;
	    }
		final int nxtL = l + 1;
		if (nxtL < LONG_RECURSION) {
			sort(a, from, to, nxtL, bucket);
			if (l == 0) {
				int lft, rgt;
				lft = from - 1; rgt = to;
				while (rgt - lft > 1) {
					int mid = lft + rgt >> 1;
					if (a[mid] < 0) lft = mid;
					else rgt = mid;
				}
				reverse(a, from, rgt);
				reverse(a, rgt, to);
	        }
	    }
	}

	public static void reverse(int[] array) {
		reverse(array, 0, array.length);
	}

	public static void reverse(int[] array, int fromIndex, int toIndex) {
		for (-- toIndex;fromIndex < toIndex;++ fromIndex, -- toIndex) {
			int swap = array[fromIndex];
			array[fromIndex] = array[toIndex];
			array[toIndex] = swap;
		}
	}

	public static void reverse(long[] array) {
		reverse(array, 0, array.length);
	}

	public static void reverse(long[] array, int fromIndex, int toIndex) {
		for (-- toIndex;fromIndex < toIndex;++ fromIndex, -- toIndex) {
			long swap = array[fromIndex];
			array[fromIndex] = array[toIndex];
			array[toIndex] = swap;
		}
	}

	public static void shuffle(int[] array) {
		java.util.Random rnd = new java.util.Random();
		for (int i = 0;i < array.length;++ i) {
			int j = rnd.nextInt(array.length - i) + i;
			int swap = array[i];
			array[i] = array[j];
			array[j] = swap;
		}
	}

	public static void shuffle(long[] array) {
		java.util.Random rnd = new java.util.Random();
		for (int i = 0;i < array.length;++ i) {
			int j = rnd.nextInt(array.length - i) + i;
			long swap = array[i];
			array[i] = array[j];
			array[j] = swap;
		}
	}
}

class IntMath {

	public static int gcd(int a, int b) {
		while (a != 0)
			if ((b %= a) != 0) a %= b;
			else return a;
		return b;
	}

	public static int gcd(int... array) {
		int ret = array[0];
		for (int i = 1; i < array.length; ++i)
			ret = gcd(ret, array[i]);
		return ret;
	}

	public static long gcd(long a, long b) {
		while (a != 0)
			if ((b %= a) != 0) a %= b;
			else return a;
		return b;
	}

	public static long gcd(long... array) {
		long ret = array[0];
		for (int i = 1; i < array.length; ++i)
			ret = gcd(ret, array[i]);
		return ret;
	}

	public static long lcm(long a, long b) {
		return a / gcd(a, b) * b;
	}

	public static int pow(int a, int b) {
		int ans = 1;
		for (int mul = a; b > 0; b >>= 1, mul *= mul)
			if ((b & 1) != 0) ans *= mul;
		return ans;
	}

	public static long pow(long a, long b) {
		long ans = 1;
		for (long mul = a; b > 0; b >>= 1, mul *= mul)
			if ((b & 1) != 0) ans *= mul;
		return ans;
	}

	public static int pow(int a, long b, int mod) {
		if (b < 0) b = b % (mod - 1) + mod - 1;
		long ans = 1;
		for (long mul = a; b > 0; b >>= 1, mul = mul * mul % mod)
			if ((b & 1) != 0) ans = ans * mul % mod;
		return (int)ans;
	}

	public static int pow(long a, long b, int mod) {
		return pow((int)(a % mod), b, mod);
	}

	public static int floorsqrt(long n) {
		return (int)Math.sqrt(n + 0.1);
	}

	public static int ceilsqrt(long n) {
		return n <= 1 ? (int)n : (int)Math.sqrt(n - 0.1) + 1;
	}
}
