CF BUDDY
← Problems·

1743F · Intersection and Union

2300 · data structures, dp, matrices

Problem: You are given nn segments on the coordinate axis. The ii-th segment is [li,ri][l_i, r_i]. Let's denote the set of all integer points belonging to the ii-th segment as SiS_i.

Let ABA \cup B be the union of two sets AA and BB, ABA \cap B be the intersection of two sets AA and BB, and ABA \oplus B be the symmetric difference of AA and BB (a set which contains all elements of AA and all elements of BB, except for the ones that belong to both sets).

Let [op1,op2,,opn1][\mathbin{op}_1, \mathbin{op}_2, \dots, \mathbin{op}_{n-1}] be an array where each element is either \cup, \oplus, or \cap. Over all 3n13^{n-1} ways to choose this array, calculate the sum of the following values:

(((S1 op1 S2) op2 S3) op3 S4)  opn1 Sn|(((S_1\ \mathbin{op}_1\ S_2)\ \mathbin{op}_2\ S_3)\ \mathbin{op}_3\ S_4)\ \dots\ \mathbin{op}_{n-1}\ S_n|

In this expression, S|S| denotes the size of the set SS.

Input Format: The first line contains one integer nn (2n31052 \le n \le 3 \cdot 10^5).

Then, nn lines follow. The ii-th of them contains two integers lil_i and rir_i (0liri31050 \le l_i \le r_i \le 3 \cdot 10^5).

Output Format: Print one integer — the sum of (((S1 op1 S2) op2 S3) op3 S4)  opn1 Sn|(((S_1\ \mathbin{op}_1\ S_2)\ \mathbin{op}_2\ S_3)\ \mathbin{op}_3\ S_4)\ \dots\ \mathbin{op}_{n-1}\ S_n| over all possible ways to choose [op1,op2,,opn1][\mathbin{op}_1, \mathbin{op}_2, \dots, \mathbin{op}_{n-1}]. Since the answer can be huge, print it modulo 998244353998244353.

Sample Cases

Case 1

Input

4
3 5
4 8
2 2
1 9

Output

162

Case 2

Input

4
1 9
3 5
4 8
2 2

Output

102

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback