Gomgom and the Tournament
InterviewTime limit2sMemory limit1024 MB
Given 2^K skill scores and the prize for each round, find the expected prize Gomgom (participant 1) wins in a random bracket, modulo 998244353.
- Level
Medium5 of 10
- Topics
- Combinatorics, Probability, Math
- Solved
- No attempts yet
Problem
Gomgom is going to enter the Gomgom Dance Tournament, where participants compete to pick the best Gomgom dancer.

The tournament has rounds. In each round, participants are paired off in twos and dance against each other.
In the -th round (), the winner receives won in prize money. If it is not the final round, the winner advances to the next round. A loser is eliminated and goes home.
Participant () has a dance skill score . When two participants meet, the one with the higher score always wins.

[Figure 1] One of the 24 possible brackets when there are 4 participants. The number in each circle is the participant number.
The bracket is chosen at random before the tournament starts, from the possible arrangements. Every arrangement is equally likely.
Gomgom learned that the prize money he can win depends on how the bracket is made.
Given the dance skill scores of Gomgom and the other participants, compute the expected prize money Gomgom can take home.
Input
The first line contains the number of rounds .
The second line contains the dance skill scores of the participants, separated by spaces.
The third line contains the prize money for each round, separated by spaces.
All dance skill scores are distinct, so no ties occur.
Gomgom is participant .
All input values are positive integers.
Output
It can be proven that the expected prize money Gomgom can earn in the tournament is always a rational number. Write the expected value as where and are coprime. There is exactly one integer with that satisfies .
Print .