logo AlgoBeat OnlineJudge
登录 注册

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

内存限制:1024 MiB 时间限制:1000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

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

Tyler likes strings, and especially those that are lexicographically less than string . After playing with magnets on the fridge he is wondering, how many distinct strings can be composed out of letters of string by rearranging them, so that the the resulting string is lexicographically less than string . 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 , that are lexicographically less than string .

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

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

Because the answer can be too large, print it modulo .

输入格式

The first line contains two integers and () --- lengths of strings and .

The second line contains integers () --- symbols of string .

The third line contains integers () --- symbols of string .

输出格式

Print the single integer --- the number of strings that are lexicographically less than , that can be composed by rearranging letters of string modulo .

样例

样例输入 1

3 4
1 2 2
2 1 2 1

样例输出 1

2

样例输入 2

4 4
1 2 3 4
4 3 2 1

样例输出 2

23

样例输入 3

4 3
1 1 1 2
1 1 2

样例输出 3

1

数据范围与提示

Note

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

In the second sample we should count all strings except , so the answer is .

In the third sample we should count only string .

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. 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
0 -- Sample tests.
1 16 0
2 15 -- --
3 11 0 -- 2
4 13 0 -- 3
5 12 -- -- In each of the strings individually all symbols are distinct
6 33 0 -- 5 Offline-evaluation.