#AGC054F. [AGC054F] Decrement

[AGC054F] Decrement

Score : 18001800 points

Problem Statement

Given is a sequence AA of NN positive integers and a sequence BB of N1N-1 positive integers. You can do the following operation any number of times.

  • Choose integers ii and jj (1i<jN1 \leq i < j \leq N) and decrease each of the following values by 11: Ai,Aj,Bi,Bi+1,,Bj1A_i,A_j,B_i,B_{i+1},\cdots,B_{j-1}. Here, there should not be any negative value after this operation.

Let mm be the maximum number of operations that can be done. Find the number, modulo 998244353998244353, of sequences that AA can be after mm operations.

Constraints

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

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \cdots ANA_N

B1B_1 B2B_2 \cdots BN1B_{N-1}

Output

Print the answer.

3
1 2 2
1 2
3

We have m=2m=2. After two operations, AA will be one of the following three sequences.

  • A=(1,0,0)A=(1,0,0): we end up with this after operations with (i,j)=(2,3)(i,j)=(2,3) and (i,j)=(2,3)(i,j)=(2,3).
  • A=(0,1,0)A=(0,1,0): we end up with this after operations with (i,j)=(1,3)(i,j)=(1,3) and (i,j)=(2,3)(i,j)=(2,3).
  • A=(0,0,1)A=(0,0,1): we end up with this after operations with (i,j)=(1,2)(i,j)=(1,2) and (i,j)=(2,3)(i,j)=(2,3).
4
1 1 1 1
2 2 2
1

Note that we do not distinguish two scenarios with different contents of BB if the contents of AA are the same.

4
2 2 3 4
3 1 4
3