**A Certain Magical Party solution codeforces** – There are 𝑛n people at a party. The 𝑖i-th person has an amount of happiness 𝑎𝑖ai.

## [Solution] A Certain Magical Party solution codeforces

Every person has a certain kind of personality which can be represented as a binary integer 𝑏b. If 𝑏=0b=0, it means the happiness of the person will increase if he tells the story to someone strictly less happy than them. If 𝑏=1b=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.

## [Solution] A Certain Magical Party solution codeforces

The first line contains a single integer 𝑛n (1≤𝑛≤2⋅1051≤n≤2⋅105) — the number of people.

The second line contains a sequence of 𝑛n integers 𝑎1,𝑎2,...,𝑎𝑛a1,a2,…,an (1≤𝑎𝑖≤2𝑛1≤ai≤2n) — the happiness values.

The third line contains a sequence of 𝑛n binary numbers 𝑏1,𝑏2,...,𝑏𝑛b1,b2,…,bn (𝑏𝑖∈{0,1}bi∈{0,1}) — the kinds of personality.

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

## [Solution] A Certain Magical Party solution codeforces

4 1 2 4 4 1 1 0 0

2

4 3 4 3 1 0 1 0 0

## [Solution] A Certain Magical Party solution codeforces

0

21 1 2 19 19 19 19 19 19 19 19 19 21 21 21 21 21 21 21 21 21 21 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1

49439766

## [Solution] A Certain Magical Party solution codeforces

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

Step 11: [1,2,4,4][1,2,4,4] →→ Person 22 tells the story to others. Since his kind of personality is 11, his happiness increases by 22 since persons 33 and 44 have strictly greater happiness.

Step 22: [1,4,4,4][1,4,4,4] →→ Person 11 tells the story to others. Since his kind of personality is 11, his happiness increases by 33 since persons 22, 33 and 44 have strictly greater happiness.

Step 33: [4,4,4,4][4,4,4,4] →→ Person 44 tells the story to others. Since his kind of personality is 00, his happiness increases by 00 since no one has strictly lesser happiness.

Step 44: [4,4,4,4][4,4,4,4] →→ Person 33 tells the story to others. Since his kind of personality is 00, his happiness increases by 00 since no one has strictly lesser happiness.

At the end, everyone has equal happiness.

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

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