#include<bits/stdc++.h>
#define ll long long
using namespace std;

inline int read(){
	int ans=0,f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){ans=ans*10+c-48;c=getchar();}
	return ans*f;
}

inline void print(ll x){
	if(x<0){putchar('-');x=-x;}
	if(x<10){putchar(x+48);return;}
	print(x/10);putchar(x%10+48);
	return;
}

const int N=1e5+10;
int C, T;

struct tt{
	int fr, en, num;
}begin[N];

const int mo=1e9+7;
ll n, m;
int hd[N], en[N], nxt[N], CNT;
int fa[N]; 

ll jc[N];
int jc_cnt;

int main(){
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	
	C=read(), T=read();
	jc[1]=1;
	jc_cnt=1;
	while(T--){
		n=read(), m=read();
		for(int i=1; i<n; i++)
			begin[i].fr=read(), begin[i].en=read(),
			begin[i].num=i;
		
		for(int i=1, w; i<=m; i++) w=read();
		
		if(C==19){
			ll ans=1;
			n--;
//			for(ll i=1; i<=n-2; i++)
//				ans=(ans*i)%mo;
			
			while(jc_cnt<=n-2){
				jc_cnt++;
				jc[jc_cnt]=jc[jc_cnt-1]*jc_cnt%mo;
			}
			
			ll w=(m*(n-1)-m*(m-1)/2)%mo;
			
			ans=(jc[n-2]*w)%mo;
			
			print(ans); putchar('\n');
			continue;
		}
		
		else if(C==18){
			puts("1");
			continue;
		}
	}
	return 0;
}
