#P16463. [UOI 2026] Color the Tree

[UOI 2026] Color the Tree

题目描述

You are given a rooted tree with nn vertices. The root of the tree is vertex 11.

For each vertex vv, you need to choose a number cvc_v equal to either 11 or 22. Such a choice of numbers will be called a coloring of the tree.

For each vertex vv, consider the unique path in the tree from vertex 11 to vertex vv. Let svs_v be the sum of all numbers cuc_u on this path, including vertices 11 and vv.

A coloring is called correct if all numbers s1,s2,,sns_1, s_2, \ldots, s_n are pairwise distinct, that is, no two of them are equal.

Count the number of correct colorings of the tree.

Since the answer may be very large, output it modulo 109+710^9 + 7.

输入格式

The first line contains one integer tt (1t104)(1 \le t \le 10^4) --- the number of test cases.

Each test case consists of two lines.

The first line of each test case contains one integer nn (2n2105)(2 \le n \le 2 \cdot 10^5) --- the number of vertices in the tree.

The second line contains n1n-1 integers p2,p3,,pnp_2, p_3, \ldots, p_n (1pi<i)(1 \le p_i < i), where pip_i is the parent of vertex ii.

This means that for each ii from 22 to nn, vertices pip_i and ii are connected by an edge, and vertex pip_i is closer to the root.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

输出格式

For each test case, output one integer --- the number of correct colorings of the tree modulo 109+710^9 + 7.

1
3
1 1
4
1
4
1 2 3
16
1
7
1 1 2 2 3 3
0

提示

In the first example, vertices 22 and 33 are children of the root.

The following correct colorings are possible: {1,1,2}\{1, 1, 2\}, {1,2,1}\{1, 2, 1\}, {2,1,2}\{2, 1, 2\}, {2,2,1}\{2, 2, 1\}.

So, the answer is 44.

Scoring

A child of vertex vv is a vertex uu such that pu=vp_u = v.

A leaf is a vertex that has no children.

The distance between two vertices of a tree is the number of edges on the unique path between them.

  • (1111 points): n10n \le 10 and t=1t = 1.
  • (66 points): each vertex has at most one child and t=1t = 1.
  • (77 points): each vertex is at distance at most 22 edges from the root and t=1t = 1.
  • (88 points): the tree has at most one vertex with exactly two children; all other vertices have at most one child, and t=1t = 1.
  • (1212 points): only the root may have more than one child and t=1t = 1.
  • (1313 points): the number of leaves does not exceed 33 and t=1t = 1.
  • (1111 points): the number of vertices with exactly two children does not exceed 2020 and t=1t = 1.
  • (1515 points): n1000n \le 1000.
  • (1717 points): No additional constraints.