electicode
HomeCoursesResourcesProblemsNational OlympiadContestsLeaderboard
...

Binary Sequences

Time Limit: 1000msMemory Limit: 256MB
View Submissions

Problem Description

We call a sequence to be binary sequence, if each its element is 000 or 111.

There are mmm conditions. The iii-th condition is described by three integers xix_ixi​, yiy_iyi​, and aia_iai​.

Consider the first xix_ixi​ elements of the sequence. This condition is satisfied if exactly yiy_iyi​ of them are equal to 111. If it is satisfied, it adds aia_ia to the total score.

For given integers rrr and bbb, define F(r,b)F(r,b)F(r,b) as the maximum total score among all binary sequences of size nnn that has at most rrr elements equal to 111 and at most bb elements equal to .

Compute ∑r=0n∑b=n−rnF(r,b) mod 998244353.\sum_{r=0}^{n} \sum_{b=n-r}^{n} F(r,b) \bmod 998244353.∑r=0n​∑b=n−rn​F

Input Format

The first line contains two integers nnn and mmm (1≤n≤109,1≤m≤2⋅105)(1 \le n \le 10^9, 1 \le m \le 2 \cdot 10^5)(1≤n≤109,1≤m≤2.

The second line contains mmm integers x1,x2,…,xmx_1, x_2, \dots, x_mx1​,x2​,…,xm​ .

The third line contains mmm integers y1,y2,…,ymy_1, y_2, \dots, y_my1​,y2​,…,ym​ .

The fourth line contains mmm integers a1,a2,…,ama_1, a_2, \dots, a_ma1​,a2​,…,am​ .

Output Format

Print one integer: the value

∑r=0n∑b=n−rnF(r,b) mod 998244353.\sum_{r=0}^{n} \sum_{b=n-r}^{n} F(r,b) \bmod 998244353.∑r=0n​∑b=n−rn​F

Examples

Example 1
Input
2
5 5
3 2 5 5 2
2 2 1 0 1
8 10 5 6 6
5 5
5 4 5 4 1
4 3 1 0 0
4 9 8 9 7
Output
344
415

© 2026 Electicode. All rights reserved.

i
​
b
000
(
r
,
b
)
mod
998244353.
⋅
105)
(1≤xi≤n)(1 \le x_i \le n)
(1≤xi​≤n)
(0≤yi≤xi)(0 \le y_i \le x_i)
(0≤yi​≤xi​)
(0≤ai≤109)(0 \le a_i \le 10^9)
(0≤ai​≤109)
(
r
,
b
)
mod
998244353.