#include<bits/stdc++.h>
using namespace std;
using i64=long long;
constexpr int mxn{100005},p{1000000007};
int c,T,n,k,e[mxn];
vector<int>tree[mxn];
i64 qpow(i64 a,i64 b)
{
	i64 res=1;
	for(;b;b>>=1,a=a*a%p)
		if(b&1)
			res=res*a%p;
	return res;
 } 
int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin>>c>>T;
    while(T--)
    {
        cin>>n>>k;
        for(int i=1,u,v;i<n;++i)
        {
			cin>>u>>v;--u;--v;
            tree[u].push_back(v);
            tree[v].push_back(u);
        }
        for(int i=0;i<k;++i)
        	cin>>e[k];
        if(c==18)
        {
            cout<<"1\n";
            continue;
        }
        if(19<=c&&c<=21)
        {
        	cerr<<"n="<<n<<'\n';
        	if(n<3)
        		cout<<"1\n";
        	else
            	cout<<qpow(n-1,n-3)<<'\n';
    		continue;
        }
        
    }
}
