luogu#P4978. 赌神:决斗

赌神:决斗

Background

God of Gambling CYJian\mathcal{CYJian} is back!

Problem Description

tomoo\mathcal{tomoo} decides to duel with CYJian\mathcal{CYJian}!

It is known that tomoo\mathcal{tomoo} has N\mathcal{N} playing cards, and each card has an RP\mathcal{RP} value Ai\mathcal{A_i}. CYJian\mathcal{CYJian} has M\mathcal{M} playing cards, and each card has an RP\mathcal{RP} value Bi\mathcal{B_i}.

CYJian\mathcal{CYJian} and tomoo\mathcal{tomoo} will each choose an arbitrary contiguous interval of cards from their own decks to duel. Whoever has the larger sum of RP\mathcal{RP} values within their chosen interval wins. Please help compute the probability that tomoo\mathcal{tomoo} wins.

Input Format

  • The first line contains two positive integers N,M\mathcal{N,M}.
  • The second line contains NN positive integers Ai\mathcal{A_i}.
  • The third line contains MM positive integers Bi\mathcal{B_i}.

Output Format

Output one number representing the probability that tomoo\mathcal{tomoo} wins. If the answer can be written in the form PQ\frac{P}{Q}, then output PQ%998244353\frac{P}{Q}\%998244353 (if you do not understand, see P3811).

5 5
1 2 3 4 5
1 3 5 7 9
754229067
10 15
7 8 5 1 2 3 6 5 4 1 
52 10 5 6 3 2 1 4 5 8 7 4 5 6 3
181952721
1 1
5
5
0
5 5
1254125 36521421 25362142 12514221 25362142
857412252 36322411 2236232 1254112 36224125
261761853
2 2
2 4
2 5
332748118

Hint

Sample Explanation

  • Sample 33: No matter how you choose, the result is always a tie on average, so the win rate is 00.
  • Sample 55: There are 99 possible ways in total, and tomoo wins 33 times, so the win rate is 1/31/3.

Constraints

  • For 20%20\% of the testdata, 0<N,M500<N,M\le50.
  • For another 20%20\% of the testdata, i=1NAi106,j=1MBj106\sum_{i=1}^NA_i\le10^6,\sum_{j=1}^MB_j\le10^6.
  • For 100%100\% of the testdata, 0<N,M2000,0<Ai,Bi1090<N,M\le2000,0<A_i,B_i\le10^9.

Translated by ChatGPT 5