#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const int mod=1e9+7;
#define int long long
inline int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')	f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
		x=x*10+c-'0',c=getchar();
	return x*f;
}
struct line{
	int from,to;
	int nxt;
};
line edge[N*3];
int head[N],edge_cnt;
vector<int> nedge[N];
inline void add_line(int u,int v){
	edge[edge_cnt].from=u;
	edge[edge_cnt].to=v;
	edge[edge_cnt].nxt=head[u];
	head[u]=edge_cnt++;
}
int C,T,n,m;
bool vis[N];
vector<pair<int,int> > vct;
int dfs(int root){
	vis[root]=1;
//	cout<<"root: "<<root<<endl;
	for(int to,i=0;i<nedge[root].size();i++){
		to=nedge[root][i];
		if(!vis[to]){
			vct.push_back(make_pair(root,to));
			dfs(to);
		}
	}
}
inline int quickpow(int a,int x){
	int ans=1;
	while(x){
		if(x&1)	ans=ans*a%mod;
		a=a*a%mod;
		x>>=1;
	}
	return ans%mod;
}
int impn[N];
signed main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	C=read();T=read();
	while(T--){
		memset(head,-1,sizeof head);
		n=read();m=read();
		for(int u,v,i=1;i<=n-1;i++){
			u=read();
			v=read();
			if(C!=18)	nedge[u].push_back(v);
			if(C!=18)	nedge[v].push_back(u);
		}	
//		cout<<"?\n";
		
//		for(auto i:vct)
//			cout<<i.first<<" "<<i.second<<endl;
		for(int i=1;i<=m;i++)
			impn[i]=read();
		if(C==18){
			cout<<1<<"\n";
			continue;
		}
		if(C>=19&&C<=21){
			int cnt=1;
			for(int i=n-1;i>=1;--i)
				cnt=(cnt+(quickpow(i,n-1-i)+i*(n-1-i)%mod)%mod)%mod;
			cout<<m*cnt%mod<<"\n";
			continue;
		}
		dfs(1);
	}
	return 0;
}

