#abc238f. F - Two Exams

F - Two Exams

Score : 500500 points

问题描述

在高桥王国中,编号为 11NNNN 名公民参加了一场编程竞赛考试。
考试分为两场,公民 ii 在第一场考试中位列第 PiP_i 名,在第二场考试中位列第 QiQ_i 名。
两场考试中均无并列名次,即序列 PPQQ 都是 (1,2,...,N)(1, 2, ..., N) 的排列。

作为王国总统的いろは(Iroha)打算从这些公民中选拔 KK 名成员组成国家队,参加即将举行的世界编程竞赛锦标赛。
国家队成员的选择必须满足以下条件:

  • 不应存在一对公民 (x,y)(x, y),其中公民 xx 被选中而公民 yy 未被选中,且满足 Px>PyP_x > P_yQx>QyQ_x > Q_y
    • 换句话说,如果公民 yy 在两场考试中的排名都小于公民 xx,那么不允许在不选择公民 yy 的情况下选择公民 xx

首先,いろは想知道满足上述条件的国家队成员选拔方案的数量。请帮助她找到这个数量。
由于这个数字可能会非常大,请将结果对 998244353998244353 取模后输出。

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

Problem Statement

In the Kingdom of Takahashi, NN citizens numbered 11 to NN took an examination of competitive programming.
There were two tests, and Citizen ii ranked PiP_i-th in the first test and QiQ_i-th in the second test.
There were no ties in either test. That is, each of the sequences PP and QQ is a permutation of (1,2,...,N)(1, 2, ..., N).

Iroha, the president of the kingdom, is going to select KK citizens for the national team at the coming world championship of competitive programming.
The members of the national team must be selected so that the following is satisfied.

  • There should not be a pair of citizens (x,y)(x, y) where Citizen xx is selected and Citizen yy is not selected such that Px>PyP_x > P_y and Qx>QyQ_x > Q_y.
    • In other words, if Citizen yy got a rank smaller than that of Citizen xx in both tests, it is not allowed to select Citizen xx while not selecting Citizen yy.

To begin with, Iroha wants to know the number of ways to select citizens for the national team that satisfy the condition above. Find it to help her.
Since this number can be enormous, print it modulo 998244353998244353.

Constraints

  • All values in input are integers.
  • 1N3001 \le N \le 300
  • 1KN1 \le K \le N
  • Each of PP and QQ is a permutation of (1,2,...,N)(1,2,...,N).

Input

Input is given from Standard Input in the following format:

NN KK

P1P_1 P2P_2 \dots PNP_N

Q1Q_1 Q2Q_2 \dots QNQ_N

Output

Print the answer as an integer.

Sample Input 1

4 2
2 4 3 1
2 1 4 3

Sample Output 1

3
  • It is fine to select Citizen 11 and Citizen 22 for the team.
  • If Citizen 11 and Citizen 33 are selected, Citizen 44 ranked higher than Citizen 33 did in both tests, so the pair (x,y)=(3,4)(x,y)=(3,4) would violate the condition in the Problem Statement.
  • It is fine to select Citizen 11 and Citizen 44.
  • If Citizen 22 and Citizen 33 are selected, the pair (x,y)=(3,1)(x,y)=(3,1) would violate the condition.
  • It is fine to select Citizen 22 and Citizen 44.
  • If Citizen 33 and Citizen 44 are selected, the pair (x,y)=(3,1)(x,y)=(3,1) would violate the condition.

The final answer is 33.

Sample Input 2

33 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

Sample Output 2

168558757

All (3316)=1166803110\binom{33}{16} = 1166803110 ways of selecting 1616 from the 3333 citizens satisfy the requirement.
Therefore, we should print 11668031101166803110 modulo 998244353998244353, that is, 168558757168558757.

Sample Input 3

15 7
4 9 7 5 6 13 2 11 3 1 12 14 15 10 8
4 14 9 12 7 15 1 2 8 11 3 5 13 6 10

Sample Output 3

23

update @ 2024/3/10 10:17:58