Fishing is a card game that is played with a deck of cards consisting of pairs of cards, numbered from to (the deck contains cards overall).
Three friends ( and ) play Fishing. The rules are as follows:
- Initially, each player receives cards.
- Then, each player discards all same-value pairs of cards they have.
- Rounds consisting of three steps are played until all remaining cards get discarded:
- a) passes one of his cards to (unless doesn't have any). If now has a pair of same-value cards, it gets discarded.
- b) passes one of his cards to (unless doesn't have any). If now has a pair of same-value cards, it gets discarded.
- c) passes one of his cards to (unless doesn't have any). If now has a pair of same-value cards, it gets discarded.
Task
You are given the hands of the three players at the end of step (1) . It's known that at least one pair of same-value cards has to be discarded during each round described at point (3).
Compute the number of different ways the game can play out. Since this number can be quite large, output it modulo .
Input data
The first line of input contains two integers and , where is the number of game scenarios to analyse.
The description of game scenarios follows. Each game scenario consists of three lines:
- The first line contains card values - player 's hand at the end of step (1).
- The second line contains card values - player 's hand at the end of step (1).
- The third line contains card values - player 's hand at the end of step (1).
Output data
For each game scenario, print the number of different ways the game can play out modulo on a separate line.
Constraints and clarifications
- Two ways the game can play out are considered different if during one step the current player chooses to pass a different card.
# | Points | Restrictions |
---|---|---|
1 | 10 | |
2 | 10 | |
3 | 10 | |
4 | 10 | |
5 | 10 | |
6 | 10 | |
7 | 10 | |
8 | 10 | |
9 | 10 | |
10 | 10 |
Example
stdin
1 1
1 2
3 3
2 1
stdout
2
Explanation
First, during step (2) , player discards all their cards. At this point, the players' hands are:
- no cards
There are two ways the game can play out from this point:
- passes card to . Then, passes it to . This way, discards the pair of cards with value . Then, has to pass his remaining card to , who discards it.
- passes card to . Then, passes it to . This way, discards the pair of cards with value . Then, has to pass his remaining card to , who discards it.