#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define fi first #define se second #define pb push_back #define all(x) (x).begin(), (x).end() #define sz(x) (int((x).size())) #define bit(x) (1 << (x)) #define cnt1(x) (__builtin_popcount(x)) #pragma comment(linker, "/STACK:256000000") template inline void chkmax(T& x, U y) { if (x < y) x = y; } template inline void chkmin(T& x, U y) { if (y < x) x = y; } typedef long long LL; typedef double DB; typedef pair PII; typedef vector VI; const int MX = 200005; const int MOD = 1e9+7; LL sz[MX], sum[MX], rev[MX], ans; VI con[MX], lft[MX], rgt[MX]; void add(LL &u, LL v) { u += v; if (u >= MOD) u -= MOD; } void DFS(int u) { sz[u] = 1, sum[u] = 0; int i, v; for (i = 0; i < sz(con[u]); i++) { v = con[u][i]; DFS(v); lft[u].pb(sz[u]); sz[u] = sz[u] * (sz[v] + 1) % MOD; } rgt[u].resize(sz(con[u])); LL tp = 1; for (i = sz(con[u]) - 1; i >= 0; i--) { v = con[u][i]; rgt[u][i] = tp; tp = tp * (sz[v] + 1) % MOD; } for (i = 0; i < sz(con[u]); i++) { v = con[u][i]; add(sum[u], sum[v] * rgt[u][i] % MOD * lft[u][i] % MOD); } add(sum[u], sz[u]); add(ans, sum[u]); } void solve() { int n, u, i; scanf("%d", &n); for (i = 1; i <= n; i++) { con[i].clear(); lft[i].clear(); rgt[i].clear(); } for (u = 2; u <= n; u++) { scanf("%d", &i); con[i].pb(u); } ans = 0; DFS(1); printf("%lld\n", ans); } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); int Tc; for (scanf("%d", &Tc); Tc--; ) solve(); return 0; }