#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
const int N=1e5+5;
const ll mod=1e9+7,A=17,B=998244353;
ll qread(){
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+ch-48ll;
		ch=getchar();
	}
	return x*f;
}
void qwrite(ll x){
	if(x<0) x=-x,putchar('-');
	if(x>9) qwrite(x/10);
	putchar(x%10+48);
}
struct edge{
	ll u,v,w;
	edge(){}
	edge(ll _u,ll _v,ll _w):u(_u),v(_v),w(_w){}
}e[N<<1];
ll c,t,n,k,a[N],ans;
ll rx=19380921;
ll _fun(){
	return rx=(A*rx+B)%mod;
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
//	freopen("traverse.in","r",stdin);
//	freopen("traverse.out","w",stdout);
//  :(
	c=qread();t=qread();
	while(t--){
		n=qread();k=qread();
		ll u,v,w;
		for(int i=1;i<n;++i){
			u=qread();v=qread();w=qread();
			e[i]=edge(u,v,w);
		}
		for(int i=1;i<=k;++i) a[i]=qread();
		if(c==18) putchar('1');
		else qwrite(_fun());
		putchar('\n');
	}
	return 0;
}
/*

*/
