eolymp
bolt
Try our new interface for solving problems
Problems

Genealogic tree

Genealogic tree

There was a lot of relatives on Ira's birthday party, so that to remember them all was not so easy task. In order to obtain an overall picture, the idea to make a family tree was born. Due to error in one of tree building procedures it turned out that the tree was not binary, although the number of vertices was correct --- \textbf{2^k-1}. --- \textit{Something is wrong here. } --- Really\textit{? } --- \textit{I propose to double-check everything. } --- \textit{Specifically. } --- \textit{Divide the tree into connected parts of} \textbf{2^i} \textit{vertices, and each of us will check his part. } --- \textit{Why do you want to do that this way?} --- \textit{I do not know, I just like powers of two, and also the sum will be exactly what we need. Although there is a little problem: you can do that in a bunch of ways. } --- \textit{Yes, I remember the student's record-books, and Sudoku. How many ways are here this time? } --- \textit{You have a great memory, give me a few minutes to think about it}. --- \textit{Catch it.} \InputFile The first line contains an integer \textbf{N}=\textbf{2^k-1} --- number of vertices in the tree (\textbf{1} ≤ \textbf{k} ≤ \textbf{12}). The next line contains \textbf{N-1} integers. Integer \textbf{a_i} (\textbf{1} ≤ \textbf{a_i} < \textbf{i+1}) on \textbf{i}-th position denotes that there is an edge between vertices \textbf{i+1} and \textbf{a_i}. Vertices are numbered starting from \textbf{1}. \OutputFile Print a single integer --- the number of ways to divide the tree into exactly \textbf{k} connected blocks of sizes \textbf{1}, \textbf{2}, \textbf{4}, ..., \textbf{2^k-1}. Each vertex must belong to exactly one block.
Time limit 1 second
Memory limit 256 MiB
Input example #1
7
1 1 2 2 3 3
Output example #1
4