Third Grader's Task
Time limit1sMemory limit1024 MB
Count the rearrangements of the symbols in s that are lexicographically less than t, modulo 998244353.
- Level
Medium6 of 10
- Topics
- Combinatorics, Math
- Solved
- No attempts yet
Problem
Tyler, a little boy, noticed magnets with symbols on the kitchen fridge. The magnets can be lined up to form a string .
Tyler likes strings, especially ones that are lexicographically less than string . After playing with the magnets, he wonders how many distinct strings can be made by rearranging the letters of so that the result is lexicographically less than . Tyler is only in third grade and cannot answer this, so count the permutations of the letters of that are lexicographically less than .
A string is lexicographically less than a string if one of the following conditions holds.
- There is a position present in both strings such that the strings are equal before the -th symbol, and the -th symbol of is less than the -th symbol of .
- is a prefix of .
Because the answer can be large, print it modulo .
Input
The first line contains two integers and (), the lengths of strings and .
The second line contains integers (), the symbols of .
The third line contains integers (), the symbols of .
Output
Print one integer: the number of rearrangements of the letters of that are lexicographically less than , modulo .
Hint
In the first sample, the counted strings are [1 2 2] and [2 1 2]. The string [2 2 1] is lexicographically greater than [2 1 2 1], so it is not counted.
In the second sample, every string except [4 3 2 1] is counted, so the answer is .
In the third sample, only [1 1 1 2] is counted.