Playoff Fixing
Contest: Educational Codeforces Round 149 (Rated for Div. 2)
Difficulty rating: 2200
Tags: combinatorics, trees

$$$2^k$$$ teams participate in a playoff tournament. The teams are numbered from $$$1$$$ to $$$2^k$$$, in order of decreasing strength. So, team $$$1$$$ is the strongest one, team $$$2^k$$$ is the weakest one. A team with a smaller number always defeats a team with a larger number.

First of all, the teams are arranged in some order during a procedure called seeding. Each team is assigned another unique value from $$$1$$$ to $$$2^k$$$, called a seed, that represents its starting position in the playoff.

The tournament consists of $$$2^k - 1$$$ games. They are held as follows: the teams are split into pairs: team with seed $$$1$$$ plays against team with seed $$$2$$$, team with seed $$$3$$$ plays against team with seed $$$4$$$ (exactly in this order), and so on (so, $$$2^{k-1}$$$ games are played in that phase). When a team loses a game, it is eliminated.

After that, only $$$2^{k-1}$$$ teams remain. If only one team remains, it is declared the champion; otherwise, $$$2^{k-2}$$$ games are played: in the first one of them, the winner of the game "seed $$$1$$$ vs seed $$$2$$$" plays against the winner of the game "seed $$$3$$$ vs seed $$$4$$$", then the winner of the game "seed $$$5$$$ vs seed $$$6$$$" plays against the winner of the game "seed $$$7$$$ vs seed $$$8$$$", and so on. This process repeats until only one team remains.

After the tournament ends, the teams are assigned places according to the tournament phase when they were eliminated. In particular:

- the winner of the tournament gets place $$$1$$$;
- the team eliminated in the finals gets place $$$2$$$;
- both teams eliminated in the semifinals get place $$$3$$$;
- all teams eliminated in the quarterfinals get place $$$5$$$;
- all teams eliminated in the 1/8 finals get place $$$9$$$, and so on.

Now that we established the rules, we do a little rigging. In particular, we want:

- team $$$1$$$ (not team with seed $$$1$$$) to take place $$$1$$$;
- team $$$2$$$ to take place $$$2$$$;
- teams $$$3$$$ and $$$4$$$ to take place $$$3$$$;
- teams from $$$5$$$ to $$$8$$$ to take place $$$5$$$, and so on.

For example, this picture describes one of the possible ways the tournament can go with $$$k = 3$$$, and the resulting places of the teams:

Some seeds are already reserved for some teams (we are not the only ones rigging the tournament, apparently). We have to fill the rest of the seeds with the remaining teams to achieve the desired placements. How many ways are there to do that? Since that value might be large, print it modulo $$$998\,244\,353$$$.

Input format:
The first line contains a single integer $$$k$$$ ($$$0 \le k \le 19$$$) — there are $$$2^k$$$ teams.

The second line contains $$$2^k$$$ integers $$$a_1, a_2, \dots, a_{2^k}$$$ ($$$a_i = -1$$$ or $$$1 \le a_i \le 2^k$$$). If $$$a_i \ne -1$$$, then team $$$a_i$$$ has seed $$$i$$$. Otherwise, the seed $$$i$$$ is not reserved for any team.

All values, that are not $$$-1$$$, are distinct.

Output format:
Print a single integer — the number of ways to fill the non-reserved seeds so that the tournament goes as planned, modulo $$$998\,244\,353$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Example input:
2
1 2 3 4

Example output:
0

Example input:
2
1 3 4 2

Example output:
1

Editorial:
Let's investigate the structure of the tournament, starting from the first round.
We know that teams from $$$2^{k-1}+1$$$ to $$$2^k$$$ have to lose during this round. At the same time, there are exactly $$$2^{k-1}$$$ losers in this round. So, every pairing has to look like this: a team from $$$1$$$ to $$$2^{k-1}$$$ versus a team from $$$2^{k-1}+1$$$ to $$$2^k$$$.
Let's try to solve for zero reserved seeds — all $$$a_i = -1$$$. So, we only have to count the number of valid tournaments.
Let's try to do that starting from the top. The winner is team $$$1$$$. The final is team $$$1$$$ against team $$$2$$$, but we have an option to choose their order in the finals. Next, the semifinal. One of teams $$$3$$$ and $$$4$$$ have to play against team $$$1$$$ and another one against team $$$2$$$. So we have the following options: choose a permutation of the losers on this stage, then choose the order of teams in every game.
If we extrapolate that argument further, we will see that the stage with $$$2^i$$$ teams multiplies the answer by $$$2^{i-1}! \cdot 2^{2^{i-1}}$$$ (arrange the losers, then fix the order in pairs). The product over all stages is the answer.
In order to add the reserved seeds, we have to go back to solving bottom-up.
Consider the first stage. We want to calculate its contribution to the answer, then promote the winners of each pair to the next stage and continue solving for $$$k = k - 1$$$.
Some pairs have both teams reserved. If both or neither of the teams are from $$$1$$$ to $$$2^{k-1}$$$, then the answer is immediately $$$0$$$. Otherwise, the winner is known, so we can promote it further.
Some pairs have no teams reserved. Here, we can pick the order of the teams (multiply the answer by $$$2$$$). And we basically know the winner as well — this is marked as $$$-1$$$. The loser is yet to be determined.
Some pairs have one team reserved, and that team is from $$$1$$$ to $$$2^{k-1}$$$. That team has to win in this pair, so we know who to promote. We will determine the loser later as well.
Finally, some pairs have one team reserved, and the team is from $$$2^{k-1}+1$$$ to $$$2^k$$$. We know the loser here, but the winner is marked $$$-1$$$. Still, we can promote this $$$-1$$$ further and deal with it on the later stages.
How do we arrange the losers? Well, it's almost the same as in the case with zero reserved teams. There are some pairs that are missing losers, and there are loser teams yet to be placed. We only have to choose which team goes where. So, it's (the number of pairs with no reserved loser)!.
Why can we promote the "winner" $$$-1$$$'s furthers without dealing with them immediately? Basically, that factorial term is the only place where we assign the numbers to teams. Obviously, we want to assign each team a number exactly once. And that happens only when the team loses. Once we assign some number to it, we can uniquely trace back to where this team started, since we fixed the order of each pair beforehand.
We decrease $$$k$$$ until it's equal to $$$0$$$. The answer is still the product of the combinations for each stage.
Overall complexity: $$$O(2^k)$$$.