#P13547. [OOI 2022] Third grader's task

[OOI 2022] Third grader's task

题目描述

While looking at the kitchen fridge, little boy Tyler noticed magnets with symbols, that can be aligned into a string ss.

Tyler likes strings, and especially those that are lexicographically less than string tt. After playing with magnets on the fridge he is wondering, how many distinct strings can be composed out of letters of string ss by rearranging them, so that the the resulting string is lexicographically less than string tt. Tyler is studying only in the third grade, so he can not answer this question. Help him to calculate the number of permutations of letters of string ss, that are lexicographically less than string tt.

We call string xx lexicographically less than string yy if one of the followings conditions is fulfilled:

  • There exists such position of symbol mm that is presented in both strings, so that before mm-th symbol the strings are equal, and the mm-th symbol of string ss is less than mm-th symbol of string yy.
  • String xx is the prefix of string yy.

Because the answer can be too large, print it modulo 998244353998\,244\,353.

输入格式

The first line contains two integers nn and mm (1n,m2000001 \le n, m \le 200\,000) --- lengths of strings ss and tt.

The second line contains nn integers s1,s2,s3sns_1, s_2, s_3 \ldots s_n (1si2000001 \le s_i \le 200\,000) --- symbols of string ss.

The third line contains mm integers t1,t2,t3tmt_1, t_2, t_3 \ldots t_m (1ti2000001 \le t_i \le 200\,000) --- symbols of string tt.

输出格式

Print the single integer --- the number of strings that are lexicographically less than tt, that can be composed by rearranging letters of string ss modulo 998244353998\,244\,353.

3 4
1 2 2
2 1 2 1
2
4 4
1 2 3 4
4 3 2 1
23
4 3
1 1 1 2
1 1 2
1

提示

Note

In the first sample, we should count strings [1 2 2][1\ 2\ 2] and [2 1 2][2\ 1\ 2]. String [2 2 1][2\ 2\ 1] is lexicographically grater than string [2 1 21][2\ 1\ 2 1], so we do not count it.

In the second sample we should count all strings except [4 3 2 1][4\ 3\ 2\ 1], so the answer is 4!1=234! - 1 = 23.

In the third sample we should count only string [1 1 1 2][1\ 1\ 1\ 2].

Scoring

The testset for this problem consists of 6 test groups. You get points for a group only if your solution passes all tests from this group and from all the required groups. Offline-evaluation\textbf{Offline-evaluation} means that you will not get immediate feedback for this group and you will be able to see the outcome only after the end of the competition. Please note that it is not required to pass all sample test cases.

Group Points Additional constraints < Required groups Comment
n,mn, m si,tis_i, t_i
0 -- Sample tests.
1 16 n,m10n, m \le 10 si,ti10s_i, t_i \le 10 0
2 15 -- si,ti2s_i, t_i \le 2 --
3 11 si,ti20s_i, t_i \le 20 0 -- 2
4 13 si,ti200s_i, t_i \le 200 0 -- 3
5 12 -- -- In each of the strings individually all symbols are distinct
6 33 0 -- 5 Offline-evaluation.