atcoder#ABC291D. [ABC291D] Flip Cards

[ABC291D] Flip Cards

Score : 400400 points

Problem Statement

NN cards, numbered 11 through NN, are arranged in a line. For each i (1i<N)i\ (1\leq i < N), card ii and card (i+1)(i+1) are adjacent to each other. Card ii has AiA_i written on its front, and BiB_i written on its back. Initially, all cards are face up.

Consider flipping zero or more cards chosen from the NN cards. Among the 2N2^N ways to choose the cards to flip, find the number, modulo 998244353998244353, of such ways that:

  • when the chosen cards are flipped, for every pair of adjacent cards, the integers written on their face-up sides are different.

Constraints

  • 1N2×1051\leq N \leq 2\times 10^5
  • 1Ai,Bi1091\leq A_i,B_i \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 B1B_1

A2A_2 B2B_2

\vdots

ANA_N BNB_N

Output

Print the answer as an integer.

3
1 2
4 2
3 4
4

Let SS be the set of card numbers to flip.

For example, when S={2,3}S=\{2,3\} is chosen, the integers written on their visible sides are 1,21,2, and 44, from card 11 to card 33, so it satisfies the condition.

On the other hand, when S={3}S=\{3\} is chosen, the integers written on their visible sides are 1,41,4, and 44, from card 11 to card 33, where the integers on card 22 and card 33 are the same, violating the condition.

Four SS satisfy the conditions: {},{1},{2}\{\},\{1\},\{2\}, and {2,3}\{2,3\}.

4
1 5
2 6
3 7
4 8
16
8
877914575 602436426
861648772 623690081
476190629 262703497
971407775 628894325
822804784 450968417
161735902 822804784
161735902 822804784
822804784 161735902
48