#abc313h. Ex - Group Photo

Ex - Group Photo

Score : 650650 points

问题陈述

设有 (2N+1)(2N+1) 名人员排成两排拍摄团体照。前排有 NN 名人员,其中第 ii 个的身高为 AiA_i;后排有 (N+1)(N+1) 名人员,其中第 ii 个的身高为 BiB_i。保证这 (2N+1)(2N+1) 名人员的身高各不相同。在每一排内,我们可以自由调整人员顺序。

假设从前到后,前排人员的身高依次为 a1,a2,,aNa_1,a_2,\dots,a_N,后排人员的身高依次为 b1,b2,,bN+1b_1,b_2,\dots,b_{N+1}。若满足以下所有条件,则称这种排列方式为好的排列

  • 对于所有 i (2iN)i\ (2 \leq i \leq N),满足 ai<bia_i < b_iai1<bia_{i-1} < b_i
  • 满足 a1<b1a_1 < b_1
  • 满足 aN<bN+1a_N < b_{N+1}

在前排 N!N! 种排列方式中,有多少种(模 998244353998244353)可以通过重新排列后排人员使得排列变为好的排列方式?

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

(2N+1)(2N+1) people are forming two rows to take a group photograph. There are NN people in the front row; the ii-th of them has a height of AiA_i. There are (N+1)(N+1) people in the back row; the ii-th of them has a height of BiB_i. It is guaranteed that the heights of the (2N+1)(2N+1) people are distinct. Within each row, we can freely rearrange the people.

Suppose that the heights of the people in the front row are a1,a2,,aNa_1,a_2,\dots,a_N from the left, and those in the back row are b1,b2,,bN+1b_1,b_2,\dots,b_{N+1} from the left. This arrangement is said to be good if all of the following conditions are satisfied:

  • ai<bia_i < b_i or ai1<bia_{i-1} < b_i for all i (2iN)i\ (2 \leq i \leq N).
  • a1<b1a_1 < b_1.
  • aN<bN+1a_N < b_{N+1}.

Among the N!N! ways to rearrange the front row, how many of them, modulo 998244353998244353, are such ways that we can rearrange the back row to make the arrangement good?

Constraints

  • 1N50001\leq N \leq 5000
  • 1Ai,Bi1091 \leq A_i,B_i \leq 10^9
  • AiAj (1i<jN)A_i \neq A_j\ (1 \leq i < j \leq N)
  • BiBj (1i<jN+1)B_i \neq B_j\ (1 \leq i < j \leq N+1)
  • AiBj (1iN,1jN+1)A_i \neq B_j\ (1 \leq i \leq N, 1 \leq j \leq N+1)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \dots ANA_N

B1B_1 B2B_2 \dots BN+1B_{N+1}

Output

Print the answer as an integer.

Sample Input 1

3
1 12 6
4 3 10 9

Sample Output 1

2
  • When a=(1,12,6)a = (1,12,6), we can let, for example, b=(4,3,10,9)b = (4,3,10,9) to make the arrangement good.
  • When a=(6,12,1)a = (6,12,1), we can let, for example, b=(10,9,4,3)b = (10,9,4,3) to make the arrangement good.
  • When aa is one of the other four ways, no rearrangement of the back row (that is, none of the possible 2424 candidates of bb) makes the arrangement good.

Thus, the answer is 22.

Sample Input 2

1
5
1 10

Sample Output 2

0

Sample Input 3

10
189330739 910286918 802329211 923078537 492686568 404539679 822804784 303238506 650287940 1
125660016 430302156 982631932 773361868 161735902 731963982 317063340 880895728 1000000000 707723857 450968417

Sample Output 3

3542400

update @ 2024/3/10 08:57:03