import java.util.*;
import java.io.*;

public class tree {

	final public static long MOD = 1000000007;
	
	public static int n;
	public static ArrayList<Integer>[] g;
	public static int[] par;
	public static int[] size;
	
	public static void main(String[] args) throws Exception {
	
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		int nC = Integer.parseInt(stdin.readLine());
		
		// Process cases.
		for (int loop=0; loop<nC; loop++) {
		
			n = Integer.parseInt(stdin.readLine());
			g = new ArrayList[n];
			for (int i=0; i<n; i++)
				g[i] = new ArrayList<Integer>();
			
			// Add edges.
			for (int i=0; i<n-1; i++) {
				StringTokenizer tok = new StringTokenizer(stdin.readLine());
				int u = Integer.parseInt(tok.nextToken())-1;
				int v = Integer.parseInt(tok.nextToken())-1;
				g[u].add(v);
				g[v].add(u);
			}
			
			// Recurse, fill in parents and sizes.
			par = new int[n];
			Arrays.fill(par, -1);
			par[0] = 0;
			size = new int[n];
			go(0);
			
			// Just do the product...
			long res = 1;
			for (int i=0; i<n; i++)
				res = (res*size[i])%MOD;
			System.out.println(res);
		}
	}
	
	// Fills in parent and sizes for v.
	public static void go(int v) {
	
		// Count myself.
		int res = 1;
		
		// Look for children.
		for (Integer next: g[v]) {

			// g might store this so skip it.
			if (next.equals(par[v])) continue;
			
			// It's a child if we get here, store parent, recurse, then add to size.
			par[next] = v;
			go(next);
			res += size[next];
		}
		
		// Update size of this subtree.
		size[v] = res;
	}
}