atcoder#ARC158F. [ARC158F] Random Radix Sort
[ARC158F] Random Radix Sort
Score : points
Problem Statement
For non-negative integers and , the -th lowest digit of is the remainder when is divided by . For instance, the -th, -st, -nd, and -rd lowest digits of are , , , and , respectively.
You are given positive integers , , , and sequences of non-negative integers and .
Consider rearranging by the following process.
- Do the following times.- Choose an integer such that .
- Then, perform a stable sort on by -th lowest digit. That is, let be the subsequence of composed of all elements of whose -th lowest digits are , and replace with the concatenation of in this order.
- Choose an integer such that .
- Then, perform a stable sort on by -th lowest digit. That is, let be the subsequence of composed of all elements of whose -th lowest digits are , and replace with the concatenation of in this order.
There are ways to execute this process. How many of them end up making equal ? Find this count modulo .
Constraints
- and are equal as multisets. That is, every integer occurs the same number of times in and .
Input
The input is given from Standard Input in the following format:
Output
Print the number, modulo , of ways to execute the process that end up making equal .
3 2 3
74 42 54
42 54 74
5
Let and be the chosen in the first and second iterations, respectively. For instance, if and , then changes as follows.
- A stable sort by -th (-th) lowest digit makes .
- A stable sort by -th (-st) lowest digit makes .
Here are all the ways to execute the process and the resulting .
- : .
- : .
- : .
- : .
- : .
- : .
- : .
- : .
- : .
2 1 1
2 3
3 2
0
There is no way to satisfy the condition.
5 100 4
0 12 34 56 78
0 12 34 56 78
982924732
All ways satisfy the condition.