309735: CF1726G. A Certain Magical Party

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

A Certain Magical Party

题目描述

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.

输入输出格式

输入格式


The first line contains a single integer $ n $ ( $ 1 \leq n \leq 2 \cdot 10^5 $ ) — the number of people. The second line contains a sequence of $ n $ integers $ a_1,a_2,...,a_n $ ( $ 1 \leq a_i \leq 2n $ ) — the happiness values. The third line contains a sequence of $ n $ binary numbers $ b_1,b_2,...,b_n $ ( $ b_i \in \{0,1\} $ ) — the kinds of personality.

输出格式


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

输入输出样例

输入样例 #1

4
1 2 4 4
1 1 0 0

输出样例 #1

2

输入样例 #2

4
3 4 3 1
0 1 0 0

输出样例 #2

0

输入样例 #3

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

输出样例 #3

49439766

说明

Here is the explanation for the first example. One valid speaking order is $ [2,1,4,3] $ (here, we have written the indices of each person). Each step shows the current happiness values and results. Step $ 1 $ : $ [1,2,4,4] $ $ \rightarrow $ 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 $ : $ [1,4,4,4] $ $ \rightarrow $ 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 $ : $ [4,4,4,4] $ $ \rightarrow $ 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 $ : $ [4,4,4,4] $ $ \rightarrow $ 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 $ [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.

Input

题意翻译

## 题意翻译 ### 题目描述 聚会上有 $n$ 个人,第 $i$ 个人有一个开心指数 $a_i$ 。 每个人都有一种确定的个性,这种个性可以用一个二进制整数 $b$ 来表示。如果 $b=0$ ,那么意味着如果他将一个故事讲给一个开心指数比他低的人,他的开心指数就会增加。如果 $b=1$ ,那么意味着如果他将一个故事讲给一个开心指数比他高的人,他的开心指数就会增加。 让我们定义讲故事的顺序为从左到右。接下来发生以下过程:从左至右的每个人给除他以外的所有人听。请注意,当这发生时,**所有的快乐指数保持不变**。当这个人讲完以后,他会根据他的个性计算目前开心指数比他少/多的人数,他的开心指数会加上这个量。请注意,**只有当前的人的快乐指数增加**。 作为聚会的组织者,你不希望任何人伤心地离开。因此,你需要计算可以使得全部 $n$ 人在这个过程的最后开心指数都相同的发言顺序的数量。如果两个发言顺序中至少有一个人的位置不同,则这两个发言顺序是不同的。 ### 输入格式 第一行为一个正整数 $n (1 \leq n \leq 2\times10^5)$ ,代表了人数。 第二行为 $n$ 个正整数 $a_1 , a_2 ,...,a_n(1\leq a_i\leq 2n)$ ,代表了每个人的开心指数。 第三行为 $n$ 个二进制整数 $b_1 , b_2 ,...,b_n(b_i \in \{0,1\})$ ,代表了每个人的个性。 ### 输出格式 输出不同发言顺序的总个数由于这个数字可能很大,所以请将它模 $998244353$ 输出。

加入题单

算法标签: