// Arup Guha
// 7/10/2017
// Solution to SI@UCF Contest #2 Problem: Video Game Story
// Edited on 7/6/2019 for new format for SI@UCF 2019.

import java.util.*;
import java.math.*;

public class story {

	final public static long MOD = 1000000007L;
	final public static int MAXN = 5000;
	
	public static ArrayList[] graph;
	public static int n;
	public static int[] sizes;

	public static long[] fact;
	public static long[] invfact;

	public static void main(String[] args) {

		// Helpful pre-comp
		fact = new long[MAXN+1];
		invfact = new long[MAXN+1];
		invfact[0] = 1;
		fact[0] = 1;
		for (int i=1; i<=MAXN; i++) {
			fact[i] = (fact[i-1]*i)%MOD;
			invfact[i] = modinv(fact[i], MOD);
		}
			
		Scanner stdin = new Scanner(System.in);
		
		// Get # of cases.
		int nC = stdin.nextInt();
		
		// Process cases.
		for (int loop=0; loop<nC; loop++) {

			// Read in the input.
			n = stdin.nextInt();
			graph = new ArrayList[n];
			for (int i=0; i<n; i++)
				graph[i] = new ArrayList<Integer>();
			sizes = new int[n];
			Arrays.fill(sizes,-1);

			// Store info in reverse.
			for (int i=0; i<n-1; i++) {
				int next = stdin.nextInt()-1;
				graph[next].add(i);
			}

			// Fill in all the tree sizes.
			for (int i=0; i<n; i++)
				sizes[i] = getSize(i);

			// Here is what we want.
			System.out.println(solve(n-1));
		}
	}

	// I cheat here - returns val ^ -1 mod modval...
	public static long modinv(long val, long mymod) {
		BigInteger v = new BigInteger(""+val);
		BigInteger m = new BigInteger(""+mymod);
		BigInteger inv = v.modInverse(m);
		return inv.longValue();
	}

	// Returns the size of the tree rooted at vertex v by recursively adding all child node sizes
	// and adding itself.
	public static int getSize(int v) {
		if (sizes[v] != -1) return sizes[v];
		int res = 1;
		for (Integer kid: (ArrayList<Integer>)graph[v])
			res += getSize(kid);
		return res;
	}

	public static long solve(int v) {

		// Easy case.
		if (graph[v].size() == 0) return 1;

		// Get the size of each child tree, for hte multinomial coefficient.
		ArrayList<Integer> mySizes = new ArrayList<Integer>();
		for (Integer kid: (ArrayList<Integer>)graph[v])
			mySizes.add(sizes[kid]);

		// Each child tree is independent, so multiply these.
		long res = 1;
		for (Integer kid: (ArrayList<Integer>)graph[v])
			res = (res*solve(kid))%MOD;

		// Here we start the multinomial coefficient - just the numerator.
		res = (res*fact[sizes[v]-1])%MOD;

		// Here we divide by each frequency for the denominator of the multinomial coefficent.
		for (Integer kid: (ArrayList<Integer>)graph[v])
			res = (res*invfact[sizes[kid]])%MOD;

		return res;
	}
}