Odd Namori

AtCoder
IDagc070_b
Time2000ms
Memory256MB
Difficulty
You are given a rooted tree $T$ with $N$ vertices labeled from $1$ to $N$. The root is the vertex $1$, and the parent of vertex $i$ $(2 \leq i \leq N)$ is $p_i$ $(p_i \lt i)$. Consider a directed graph $G$ with $N$ vertices labeled from $1$ to $N$. We call $G$ **good** if it satisfies all of the following conditions (here, let $u \to v$ denote an edge from vertex $u$ to vertex $v$): * Every vertex in $G$ has outdegree $1$. * $G$ has no cycle of even length. * For every $i$ $(2 \leq i \leq N)$, $G$ does not contain the edge $i \to p_i$. Find the sum, modulo $998244353$, of $2^{(\text{number of cycles in }G)}$ over all good graphs $G$. ## Constraints * $2 \leq N \leq 10^5$ * $1 \leq p_i \lt i$ * All values given in the input are integers. ## Input The input is given from Standard Input in the following format: $N$ $p_2$ $p_3$ $\dots$ $p_N$ [samples]
Samples
Input #1
2
1
Output #1
6

Here are the two possible good graphs:

*   The graph with edges $1 \to 1$ and $2 \to 2$.
*   The graph with edges $1 \to 2$ and $2 \to 2$.

The number of cycles in these graphs are $2$ and $1$, respectively. Thus, the answer is $(2^2 + 2^1) \bmod{998244353} = 6$.
Input #2
3
1 2
Output #2
34

For example, the graph with edges $1 \to 2$, $2 \to 3$, and $3 \to 1$ is a good graph, whose number of cycle is $1$.
Input #3
5
1 2 1 1
Output #3
3104
Input #4
20
1 2 2 2 5 3 5 1 7 9 4 6 4 12 8 2 5 16 6
Output #4
784973196

Remember to find the sum modulo $998244353$.
API Response (JSON)
{
  "problem": {
    "name": "Odd Namori",
    "description": {
      "content": "You are given a rooted tree $T$ with $N$ vertices labeled from $1$ to $N$. The root is the vertex $1$, and the parent of vertex $i$ $(2 \\leq i \\leq N)$ is $p_i$ $(p_i \\lt i)$. Consider a directed grap",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "agc070_b"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a rooted tree $T$ with $N$ vertices labeled from $1$ to $N$. The root is the vertex $1$, and the parent of vertex $i$ $(2 \\leq i \\leq N)$ is $p_i$ $(p_i \\lt i)$.\nConsider a directed grap...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments