#P5326. [ZJOI2019] 开关

[ZJOI2019] 开关

Description

Jiutiao Keliang is a playful girl.

One day, she and her good friend Brother Fahai went to play an escape room. In front of them are nn switches, and at the beginning every switch is in the off state. To pass this level, the switches must reach a specified state. The target state is given by a binary array ss of length nn. si=0s_i = 0 means the ii-th switch needs to be off in the end, and si=1s_i = 1 means the ii-th switch needs to be on in the end.

However, as challengers, Keliang and Fahai do not know ss. So they decided to use a safer method: pressing randomly. Based on the shape and position of the switches, they used some “mystic” methods to assign each switch a weight pip_i (pi>0p_i > 0). Each time, they choose and press one switch with probability proportional to pip_i (the probability that the ii-th switch is chosen is pij=1npj\frac{p_i}{\sum^n_{j=1}p_j}). After a switch is pressed, its state is flipped: on becomes off, and off becomes on. Note that each round of selection is completely independent.

During the process of pressing switches, once the current states of switches match ss, the door in front of Keliang and Fahai will open. They will immediately stop pressing switches and move on to the next level. As a very lucky person, Keliang opened the door after pressing i=1nsi\sum^n_{i=1} s_i times. To feel how good her luck was, she wants you to help her compute: using this random pressing method, what is the expected number of presses needed to pass this level?

Input Format

The first line contains an integer nn, representing the number of switches.
The second line contains nn integers sis_i (si{0,1}s_i\in \{0,1\}), representing the target state of each switch.
The third line contains nn integers pip_i, representing the weight of each switch.

Output Format

Output one line with one integer, representing the answer modulo 998244353998244353. That is, if the answer in simplest fractional form is xy\frac{x}{y} (x0x \ge 0, y1y \ge 1, gcd(x,y)=1\gcd(x, y) = 1), you should output x×y1mod998244353x \times y^{-1} \bmod 998244353.

2
1 1
1 1
4
8
1 1 0 0 1 1 0 0
1 2 3 4 5 6 7 8
858924815

Hint

[Sample Explanation #1]

In the first two presses, there is a probability of 12\frac12 to reach ss, and a probability of 12\frac12 to return to the initial state. Therefore, the expected number of switch presses is:

$$\sum^{+\infty}_{i=1}2i\times \left(\frac12\right)^i=4$$

[Constraints and Notes]

Test Point nn Other Notes Test Point nn Other Notes
11 =2=2 None 66 100\le 100 pi2,si=1p_i\le 2,s_i=1
22 77
33 8\le 8 88 pi2×103\sum p_i\le 2\times 10^3
44 99
55 100\le 100 pi=1p_i=1 1010 None

For 100%100\% of the testdata, it is guaranteed that n1n\ge1, i=1npi5×104\sum^n_{i=1}p_i\le5\times10^4, and pi1p_i\ge1.

Translated by ChatGPT 5