Public Judge

pjudge

Limite de temps : 2 s Limite de mémoire : 512 MB Points totaux : 100 Hackable ✓

#21750. 【PR #8】Dissipating Sorrows

Statistiques

For two permutations $p$ and $q$, a binary string $s$ is called "xiaochou" if and only if there exists a $2 \times n$ matrix $a$ such that:

  1. Every element from $1$ to $2n$ appears in the matrix exactly once.
  2. $a_{1,i} < a_{1,j}$ if and only if $p_i < p_j$.
  3. $a_{2,i} < a_{2,j}$ if and only if $q_i < q_j$.
  4. $a_{1,i} < a_{2,i}$ if and only if $s_i = 0$.

Let $f(p, q)$ be the number of binary strings $s$ that are "xiaochou" for these two permutations.

Given a partial permutation $q$ and a full permutation $p$, calculate the sum of $f(p, q)$ over all possible ways to complete $q$.

Input

The first line contains an integer $n$, the length of the permutations.

The second line contains $n$ integers representing the permutation $p$.

The third line contains $n$ integers representing the partial permutation $q$, where $q_i = 0$ indicates that the value at this position is unknown.

Output

Output the sum of $f(p, q)$ modulo $998244353$.

Examples

Input 1

2
1 2
2 1

Output 1

3

Note 1

00 corresponds to:

1 2
4 3

11 corresponds to:

3 4
2 1

01 corresponds to:

1 4
3 2

Input 2

4
4 3 2 1
4 3 2 1

Output 2

16

Input 3

5
1 2 3 4 5
0 0 0 0 0

Output 3

1546

Input 4

6
1 6 2 5 3 4
0 1 0 2 0 3

Output 4

52

Constraints

For $100\%$ of the data, $1 \leq n \leq 100$, $1 \leq p_i \leq n$, $0 \leq q_i \leq n$. For $i \neq j$, $p_i \neq p_j$, and if $q_i, q_j \neq 0$, then $q_i \neq q_j$.

Test Case ID $n \leq$ Special Properties
$1 \sim 4$ $5$ None
$5 \sim 9$ $100$ $q_i \neq 0$
$10 \sim 14$ $100$ $q_i = 0$
$15 \sim 20$ $100$ None

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.