#P8194. [USACO22FEB] Phone Numbers P
[USACO22FEB] Phone Numbers P
Problem Description
Bessie has gotten a new phone with a 9-key keypad, laid out as follows:
123
456
789
Bessie is in a hurry and is trying to type a given phone number, so she decides to save time by using one of her hooves to press multiple buttons at once. Specifically, Bessie's hoof may press a single key, two keys that share an edge (there are such possibilities), or the four keys that form a square (, , , ).
For example, if the phone number Bessie wants to type is , she might try to save time by pressing keys in the following way:
- Press and at the same time.
- Press .
- Press at the same time.
- Press and at the same time.
Unfortunately, Bessie greatly overestimated her ability to do this task—if Bessie's hoof presses multiple keys at the same time, then all of those keys will be entered in an arbitrary order. So if Bessie tries to press keys in the sequence above, the phone number she ends up typing could be or (or any other possible sequence).
Given a sequence that Bessie has already typed, compute the number of phone numbers she might have intended to type, modulo .
Input Format
The first line contains an integer (), representing the number of independent test cases.
The next lines each contain a non-empty digit string consisting only of digits from to . It is guaranteed that the total length of all digit strings in the input does not exceed .
Output Format
For each test case, output the number of phone numbers Bessie might have intended to type, modulo .
5
1478
4455
5968
31313211
123659874
5
2
24
3
255
Hint
Sample Explanation
For the first test case, Bessie might have intended to type one of the following five phone numbers:
1478
1487
4178
4187
1748
For example, if Bessie intended to type , she could have tried to press and at the same time, and then press and at the same time.
For the third test case, since these digits form a square, any permutation of the typed sequence could be the phone number Bessie intended.
Constraints
- For test cases , the length of every phone number is at most .
- For test cases , each phone number contains only and .
- For test cases , each phone number does not contain .
- For test cases , each phone number contains only .
- For test cases , the total length of all phone numbers does not exceed .
- For test cases , the total length of all phone numbers does not exceed .
- For test cases , the total length of all phone numbers does not exceed .
- For test cases , there are no additional constraints.
Translated by ChatGPT 5