QOJ.ac

QOJ

时间限制: 1 s 内存限制: 512 MB 总分: 100

#14460. Left Child Right Sibling

统计

Little C learned about the "left-child right-sibling representation" in a data structures class. For a rooted multi-way tree, we can convert it into a binary tree using this representation. Formally, given a rooted tree $T$ with $n$ nodes, labeled $1, 2, \dots, n$, where node $1$ is the root, we convert it into a binary tree $T'$ that also contains $n$ nodes and is rooted at node $1$ using the following representation: For each node $u$ in the tree, suppose its children are $v_1, v_2, \dots, v_k$.

  • You can specify any order for these children, denoted as $v'_1, v'_2, \dots, v'_k$.
  • In the tree $T'$, the left child of node $u$ is $v'_1$; for any $i \in [1, k-1]$, the right child of node $v'_i$ is $v'_{i+1}$.

We define two binary trees as different if and only if there exists a node $u$ such that its left child is different in the two trees (or one has a left child and the other does not), or its right child is different in the two trees (or one has a right child and the other does not). We define the subtree size of a node as the number of nodes in its subtree (including itself).

Little C noticed that under this representation, there are many possible forms for the final tree $T'$. Now, given a tree $T$, among all possible trees $T'$ that can be obtained using the "left-child right-sibling representation," Little C defines a "good representation" as one where the sum of the subtree sizes of all nodes is minimized. Little C wants to know the sum of the subtree sizes of all nodes for a "good representation" $T'$, and how many different "good representations" exist.

Input

The first line contains an integer $n$ ($1 \le n \le 10^5$), representing the number of nodes in the tree. The second line contains $n-1$ integers $f_2, f_3, \dots, f_n$ ($1 \le f_i < i$), where $f_i$ represents the parent of node $i$.

Output

Output two lines, each containing an integer. The first line represents the sum of the subtree sizes of all nodes in a "good representation" $T'$. The second line represents the number of "good representations" $T'$, modulo $998244353$.

Examples

Input 1

5
1 1 1 2

Output 1

13
2

Input 2

5
1 1 1 1

Output 2

15
24

Note

For the first example, all possible trees $T'$ obtained from $T$ using the "left-child right-sibling representation" are shown below:

All possible T' under the left-child right-sibling representation

  • The sum of the subtree sizes of all nodes for the two trees $T'$ in the first column is $5 + 4 + 2 + 1 + 1 = 13$.
  • The sum of the subtree sizes of all nodes for the two trees $T'$ in the second column is $5 + 3 + 4 + 1 + 1 = 14$.
  • The sum of the subtree sizes of all nodes for the two trees $T'$ in the third column is $5 + 2 + 3 + 4 + 1 = 15$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.