Third grader's task

시간 제한1초메모리 제한1024 MB

요약
길이 200000 이하이고 값이 200000 이하인 수열 s의 문자를 재배열해 만들 수 있는 순열 중 t보다 사전순으로 작은 것의 개수를 998244353으로 나눈 나머지를 구한다.
난이도

어려움10점 중 8점

유형
조합론, 수학, 구현, 정렬
정답자
아직 제출이 없습니다

문제

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 998,244,353998\\,244\\,353.

입력

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

The second line contains nn integers s_1,s_2,s_3…s_ns\_1, s\_2, s\_3 \ldots s\_n (1≤s_i≤200,0001 \le s\_i \le 200\\,000) --- symbols of string ss.

The third line contains mm integers t_1,t_2,t_3…t_mt\_1, t\_2, t\_3 \ldots t\_m (1≤t_i≤200,0001 \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 998,244,353998\\,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].

예제3

  1. 예제 1

    입력
    3 4
    1 2 2
    2 1 2 1
    
    예상 출력
    2
    
  2. 예제 2

    입력
    4 4
    1 2 3 4
    4 3 2 1
    
    예상 출력
    23
    
  3. 예제 3

    입력
    4 3
    1 1 1 2
    1 1 2
    
    예상 출력
    1