Third grader's task | 프로그래밍의 벗 PivotOJ
PivotOJ

Third grader's task

시간 제한: 1000ms메모리 제한: 1024MB출처: MOOI 2021-22 finalBOJ 30674

문제

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.

힌트

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

In the second sample we should count all strings except [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

입력
3 4
1 2 2
2 1 2 1
출력
2

예제 2

입력
4 4
1 2 3 4
4 3 2 1
출력
23

예제 3

입력
4 3
1 1 1 2
1 1 2
출력
1
코드를 제출하려면 로그인하세요.