#P1648C. Tyler and Strings
Tyler and Strings
Description
While looking at the kitchen fridge, the little boy Tyler noticed magnets with symbols, that can be aligned into a string .
Tyler likes strings, and especially those that are lexicographically smaller than another 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 resulting string is lexicographically smaller than the string ? Tyler is too young, so he can't answer this question. The alphabet Tyler uses is very large, so for your convenience he has already replaced the same letters in and to the same integers, keeping that different letters have been replaced to different integers.
We call a string lexicographically smaller than a 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 smaller than -th symbol of string .
- String is the prefix of string and .
Because the answer can be too large, print it modulo .
The first line contains two integers and () — the lengths of strings and respectively.
The second line contains integers () — letters of the string .
The third line contains integers () — letters of the string .
Print a single number — the number of strings lexicographically smaller than that can be obtained by rearranging the letters in , modulo .
Input
The first line contains two integers and () — the lengths of strings and respectively.
The second line contains integers () — letters of the string .
The third line contains integers () — letters of the string .
Output
Print a single number — the number of strings lexicographically smaller than that can be obtained by rearranging the letters in , modulo .
Note
In the first example, the strings we are interested in are and . The string is lexicographically larger than the string , so we don't count it.
In the second example, all strings count except , so the answer is .
In the third example, only the string counts.