시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB5000.000%

문제

The new academic year has just started! Everyone is super excited about learning new stuff in Hogwarts. Harry Potter is attending a new class about spells, where he is learning how to read people’s minds and determine their next actions. Harry is going to play a game with Hermione and he would like to try using the new spell to win the game.

Harry and Hermione are going to play Bingo! Each player will have a 5 × 5 board that contains the numbers from 1 to 25 (each number appearing exactly once). The game goes in turns and Harry starts. Each player says a number (that was not marked yet) out loud and both players mark this number on their boards. The first player to fill up 5 complete rows, columns, or diagonals shouts Bingo! and wins the game. If both of them shout at the same time, it will be considered a tie and no one wins.

As Harry can read Hermione’s mind, he can see her board configuration and can know what is the sequence of numbers she is going to shout. This shall be a permutation of the 25 numbers that she is going to call out (of course she will skip numbers that Harry already shouted!). Ron can not leave them playing alone! He asked Harry a mathematics question. In how many ways can you win this game? Two ways are considered different if at the i-th turn he would shout a different number. As this number might be large, print it modulo 1,000,000,007.

입력

Your program will be tested on one or more test cases. The first line of the input will be a single integer T, the number of test cases (1 ≤ T ≤ 50), followed by T test cases.

Each test case will be presented on multiple lines. The first 5 lines represent Harry’s board, where each line contains 5 integers separated by single spaces. The second 5 lines represent Hermione’s board in the same format. The last line will contain a permutation of the numbers from 1 to 25 separated by single spaces representing Hermione’s shout outs. It’s guaranteed that each number from 1 to 25 will appear only once in Harry’s and Hermione’s boards and in Hermione’s shout outs.

출력

For each test case, print a single line containing the number of ways Harry can win the game modulo 1,000,000,007.

예제 입력 1

2
16 22 11 21 15
18 24 13 14 25
8 9 7 2 4
3 19 23 17 12
20 5 1 6 10
24 17 21 8 10
15 2 16 25 7
13 12 3 14 9
19 1 6 4 20
23 22 5 11 18
15 18 16 17 13 19 1 24 23 21 20 4 9
11 2 12 22 7 8 3 25 14 5 6 10
20 11 22 2 19
17 25 23 14 13
10 8 6 21 18
12 1 5 24 16
7 4 15 3 9
18 10 20 12 11
1 23 13 8 15
6 7 9 19 24
25 21 3 17 2
14 22 4 16 5
9 13 6 24 5 21 10 22 25 11 23 15 17 8
19 2 20 18 7 4 16 12 1 3 14

예제 출력 1

775283109
200384431