Under construction

Still being built. Things may change

Gomgom and the Tournament

Interview

Time limit2sMemory limit1024 MB

Summary
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 2K2^K participants compete to pick the best Gomgom dancer.

곰곰 짜잔

The tournament has KK rounds. In each round, participants are paired off in twos and dance against each other.

In the ii-th round (1≤i≤K1 \leq i \leq K), the winner receives r_ir\_i 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 ii (1≤i≤2K1 \leq i \leq 2^K) has a dance skill score a_ia\_i. When two participants meet, the one with the higher score always wins.

4-1-3-2 order bracket

[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 (2K)!(2^K)! 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 KK. (1≤K≤18)(1 \leq K \leq 18)

The second line contains the dance skill scores a_1,...,a_2Ka\_1, ..., a\_{2^K} of the participants, separated by spaces. (1≤a_i≤109)(1 \leq a\_i \leq 10^9)

The third line contains the prize money r_1,...,r_Kr\_1, ..., r\_K for each round, separated by spaces. (1≤r_i≤106)(1 \leq r\_i \leq 10^6)

All dance skill scores are distinct, so no ties occur.

Gomgom is participant 11.

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 yx\frac{y}{x} where xx and yy are coprime. There is exactly one integer zz with 0≤z≤998 244 3520 \leq z \leq 998\ 244\ 352 that satisfies xz≡y(mod998 244 353)xz \equiv y \pmod {998\ 244\ 353}.

Print zz.

Examples1

  1. Example 1

    Input
    2
    2 1 3 4
    1000 10000
    
    Expected output
    332748451