Narrower Passageway
Time limit1sMemory limit2048 MB
Each column is fogged with probability 1/2; find the expected total strength, where each unfogged segment adds min of its two row maxima (0 if equal), modulo 998244353.
- Level
Hard8 of 10
- Topics
- Stack, Probability, Dynamic programming, Array
- Solved
- No attempts yet
Problem
You are a strategist of The ICPC Kingdom. You received intel that monsters will attack a narrow passageway near the kingdom. The passageway is a grid with rows (numbered from to ) and columns (numbered from to ). Denote as the cell in row and column . Every day, a soldier with power is assigned to protect .
The passageway is very foggy. Within a day, each column has a chance of being covered in fog. If a column is covered in fog, the two soldiers assigned to that column are not deployed that day. Otherwise, the assigned soldiers are deployed.
A connected area () is a maximal set of consecutive columns from to (inclusive) such that no column in the set is covered in fog. The image below shows an example. The gray cells are covered in fog. There are connected areas: , , , and .

The strength of a connected area is computed as follows. Let and be the maximum power of the soldiers in the first and second rows of the connected area, respectively. Formally, for . If , then the strength is . Otherwise, the strength is .
The total strength of the deployment is the sum of the strengths of all connected areas. Determine the expected total strength of the deployment on any single day.
Input
The first line contains an integer ().
Each of the next two lines contains integers ().
Output
Let . It can be shown that the expected total strength can be expressed as an irreducible fraction , where and are integers and . Output an integer in a single line such that and .