## GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

# [Solution] A Certain Magical Party Codeforces Solution

G. A Certain Magical Party
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are $n$ people at a party. The $i$-th person has an amount of happiness ${a}_{i}$.

Every person has a certain kind of personality which can be represented as a binary integer $b$. If $b=0$, it means the happiness of the person will increase if he tells the story to someone strictly less happy than them. If $b=1$, it means the happiness of the person will increase if he tells the story to someone strictly more happy than them.

Let us define a speaking order as an ordering of the people from left to right. Now the following process occurs. We go from left to right. The current person tells the story to all people other than himself. Note that all happiness values stay constant while this happens. After the person is done, he counts the number of people who currently have strictly less/more happiness than him as per his kind of personality, and his happiness increases by that value. Note that only the current person's happiness value increases.

As the organizer of the party, you don't want anyone to leave sad. Therefore, you want to count the number of speaking orders such that at the end of the process all $n$ people have equal happiness.

Two speaking orders are considered different if there exists at least one person who does not have the same position in the two speaking orders.

Input

The first line contains a single integer $n$ ($1\le n\le 2\cdot {10}^{5}$) — the number of people.

Solution Click Below:-  👉
👇👇👇👇👇

The second line contains a sequence of $n$ integers ${a}_{1},{a}_{2},...,{a}_{n}$ ($1\le {a}_{i}\le 2n$)  — the happiness values.

The third line contains a sequence of $n$ binary numbers ${b}_{1},{b}_{2},...,{b}_{n}$ (${b}_{i}\in \left\{0,1\right\}$)  — the kinds of personality.

Output

Output the number of different valid speaking orders. Since this number can be large, output it modulo $998244353$.

Note

Here is the explanation for the first example. One valid speaking order is $\left[2,1,4,3\right]$ (here, we have written the indices of each person). Each step shows the current happiness values and results.

Step $1$$\left[1,2,4,4\right]$ $\to$ Person $2$ tells the story to others. Since his kind of personality is $1$, his happiness increases by $2$ since persons $3$ and $4$ have strictly greater happiness.

Step $2$$\left[1,4,4,4\right]$ $\to$ Person $1$ tells the story to others. Since his kind of personality is $1$, his happiness increases by $3$ since persons $2$$3$ and $4$ have strictly greater happiness.

Step $3$$\left[4,4,4,4\right]$ $\to$ Person $4$ tells the story to others. Since his kind of personality is $0$, his happiness increases by $0$ since no one has strictly lesser happiness.

Step $4$$\left[4,4,4,4\right]$ $\to$ Person $3$ tells the story to others. Since his kind of personality is $0$, his happiness increases by $0$ since no one has strictly lesser happiness.

At the end, everyone has equal happiness.

Note that $\left[2,1,3,4\right]$ is also a valid answer for this example.

It can be shown that there is no valid ordering for the second example.