题目描述
Problem Description
There is an integer sequence $ S $ of length $ N $. Initially, all elements of $ S $ are $ 0 $.
You are also given two integer sequences of length $ Q $: $P = (P_1, P_2, \dots, P_Q)$ and $V = (V_1, V_2, \dots, V_Q)$.
Snuke wants to perform $ Q $ operations on sequence $ S $ in order. The $ i $-th operation is as follows:
- Perform one of the following operations:
- Replace $S_1, S_2, \dots, S_{P_i}$ with $ V_i $. However, Snuke will cry if there is any element in $S_1, S_2, \dots, S_{P_i}$ that is strictly greater than $ V_i $ before this operation.
- Replace $S_{P_i}, S_{P_i+1}, \dots, S_N$ with $ V_i $. However, Snuke will cry if there is any element in $S_{P_i}, S_{P_i+1}, \dots, S_N$ that is strictly greater than $ V_i $ before this operation.
Find the number of ways to perform all $ Q $ operations without making Snuke cry, modulo $ 998244353 $.
Two operation sequences are considered different if there exists an $1 \leq i \leq Q$ such that the $ i $-th operation choice is different.
Input Format
The input is given from the standard input in the following format:
$ N $ $ Q $ $ P_1 $ $ V_1 $ $ P_2 $ $ V_2 $ $\vdots$ $ P_Q $ $ V_Q $
Output Format
Output the answer as an integer.
Sample Input and Output
Sample Input #1
8 3
1 8
8 1
2 1
Sample Output #1
1
Sample Input #2
8 3
8 1
1 8
1 2
Sample Output #2
0
Sample Input #3
241 82
190 3207371
229 3639088
61 4428925
84 17258698
34 42692503
207 59753183
180 67198566
78 99285033
60 102449991
234 122146510
111 126959145
141 152331579
78 159855439
11 169658471
22 189991287
37 204602946
73 209329065
72 215363269
152 236450854
175 237822921
22 261431608
144 252550201
54 268889550
238 276997357
69 313065279
226 330144323
6 335788783
126 345410019
220 348318997
166 365778763
142 382251905
200 406191336
234 392702679
83 409660987
183 410908761
142 445707116
205 470279207
230 486436406
156 494269002
113 495687706
200 500005738
162 505246499
201 548652987
86 449551554
62 459527873
32 574001635
230 601073337
175 610244315
174 613857555
181 637452273
158 637866397
148 648101378
172 646898076
144 682578257
239 703460335
192 713255331
28 727075136
196 730768166
111 751850547
90 762445737
204 762552166
72 773170159
240 803415865
32 798873367
195 814999380
72 842641864
125 851815348
116 858041919
200 869948671
195 873324903
5 877767414
105 877710280
150 877719360
9 884707717
230 880263190
88 967344715
49 977643789
167 979463984
70 981400941
114 991068035
94 991951735
141 995762200
Sample Output #3
682155965
Notes/Hints
Constraints
- $2 \leq N \leq 5000$
- $1 \leq Q \leq 5000$
- $1 \leq P_i \leq N$
- $1 \leq V_i \leq 10^9$
- All inputs are integers.
Sample Explanation 1
The following sequence of operations prevents Snuke from crying:
- Replace $ S_1 $ with $ 8 $.
- Replace $ S_8 $ with $ 1 $.
- Replace $S_2, S_3, \dots, S_8$ with $ 1 $.
There are no other sequences of operations that satisfy the conditions, so the answer is $ 1 $. For example, if you replace $S_1, S_2, \dots, S_8$ with $ 8 $ in the first operation, Snuke will cry in the second operation regardless of the choice.
Sample Explanation 2
No matter how you perform the first two operations, Snuke will cry in the third operation.
Sample Explanation 3
Remember to find the answer modulo $ 998244353 $.
输入格式
见题面 Input 部分
输出格式
见题面 Output 部分