#include <bits/stdc++.h>
using namespace std;
long long dp[301][301][301][2][3];
long long mod = 1000000007;
long long solve(long long ft,long long fs,long long st,int done,int i){
if(ft<0||fs<0||st<0||(i==0&&done==0))return 0;
if(ft==0&&fs==0&&st==0){
return 1;
}
if(dp[ft][fs][st][done][i]!=-1)return dp[ft][fs][st][done][i];
if(i==0){
long long c1 = 0;
if(ft==0&&fs==0){
c1 = solve(ft,fs,st,0,(i+1)%3);
}
else c1 = ((solve(ft-1,fs,st+1,0,(i+1)%3)*ft)%mod)+((solve(ft,fs-1,st,1,(i+1)%3)*fs)%mod);
return dp[ft][fs][st][done][i] = c1%mod;
}if(i==1){
long long c1 = 0;
if(fs==0&&st==0){
c1 = solve(ft,fs,st,done,(i+1)%3);
}
else c1 = ((solve(ft+1,fs-1,st,done,(i+1)%3)*fs)%mod)+((solve(ft,fs,st-1,1,(i+1)%3)*st)%mod);
return dp[ft][fs][st][done][i] = c1%mod;
}if(i==2){
long long c1 = 0;
if(ft==0&&st==0){
c1 = solve(ft,fs,st,done,(i+1)%3);
}
else c1 = ((solve(ft,fs+1,st-1,done,(i+1)%3)*st)%mod)+((solve(ft-1,fs,st,1,(i+1)%3)*ft)%mod);
return dp[ft][fs][st][done][i] = c1%mod;
}
}
int main(){
memset(dp,-1,sizeof dp);
int n,t;cin>>n>>t;
while(t--){
set<int> a, b,c;
for(int i = 0;i<2*n;i++){
int x;cin>>x;a.insert(x);
}
for(int i = 0;i<2*n;i++){
int x;cin>>x;b.insert(x);
}
for(int i = 0;i<2*n;i++){
int x;cin>>x;c.insert(x);
}
int cnt[3*n+1] = {0};
for(auto i:a)cnt[i]|=(1<<0);
for(auto i:b)cnt[i]|=(1<<1);
for(auto i:c)cnt[i]|=(1<<2);
int fs =0 , ft = 0 , st = 0;
for(int i = 1;i<=3*n;i++){
if(cnt[i]==3){
fs++;
}
if(cnt[i]==5){
ft++;
}
if(cnt[i]==6){
st++;
}
}
cout<<solve(ft,fs,st,1,0)<<endl;
}
}