P28971 [洛谷] [ZJOI2019] Switches
省选
题目描述

Problem Description

Kujou Karen is a playful girl.

One day, she and her good friend Fahai went to an escape room. In front of them are $n$ switches, all initially in the off state. To pass this level, they need to set the switches to a specified state. The target state is given by an array $s$ of length $n$, where $s_i = 0$ means the $i$-th switch needs to be off, and $s_i = 1$ means the $i$-th switch needs to be on.

However, as challengers, Karen and Fahai do not know $s$. Therefore, they decide to adopt a conservative approach: random pressing. They assign a weight $p_i$ ($p_i > 0$) to each switch based on its appearance and position, and each time they select a switch to press with a probability proportional to $p_i$ (the probability of selecting the $i$-th switch is $\frac{p_i}{\sum_{j=1}^n p_j}$). When a switch is pressed, its state is inverted, i.e., 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 state of the switches matches $s$, the door in front of Karen and Fahai will open, and they will immediately stop pressing switches and proceed to the next level. As a lucky person, Karen managed to open the door after pressing $\sum_{i=1}^n s_i$ times. To appreciate her luck, she wants you to calculate the expected number of switch presses needed to pass this level using this random method.

Input Format

The first line contains an integer $n$, the number of switches.
The second line contains $n$ integers $s_i$ ($s_i \in \{0, 1\}$), the target state of the switches.
The third line contains $n$ integers $p_i$, the weights of the switches.

Output Format

Output a single integer, the answer modulo $998244353$. If the answer is represented as a fraction $\frac{x}{y}$ ($x \ge 0$, $y \ge 1$, $\gcd(x, y) = 1$), you need to output $x \times y^{-1} \bmod 998244353$.

Sample Input and Output

Sample Input #1

2
1 1
1 1

Sample Output #1

4

Sample Input #2

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

Sample Output #2

858924815

Notes/Hints

[Sample Explanation #1]

In the first two presses, there is a $\frac{1}{2}$ probability of achieving $s$, and a $\frac{1}{2}$ probability of returning to the original state. Therefore, the expected number of switch presses is:

$$\sum_{i=1}^{+\infty} 2i \times \left(\frac{1}{2}\right)^i = 4$$


[Data Range and Constraints]

Test Case $n$ Additional Constraints Test Case $n$ Additional Constraints
1 2 None 6 $\le 100$ $p_i \le 2, s_i = 1$
2 2 None 7 $\le 100$ $p_i \le 2, s_i = 1$
3 $\le 8$ None 8 $\le 100$ $\sum p_i \le 2 \times 10^3$
4 $\le 8$ None 9 $\le 100$ $\sum p_i \le 2 \times 10^3$
5 $\le 100$ $p_i = 1$ 10 $\le 100$ None

For $100\%$ of the data, it is guaranteed that $n \ge 1$, $\sum_{i=1}^n p_i \le 5 \times 10^4$, and $p_i \ge 1$.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
2 1 1 1 1
样例输出
4