Inherited disease
Time limit1sMemory limit16 MB
Follow the birth-order path down D generations where a generation g member has g+1 children and print each breadth-first number modulo 1000000007.
- Level
Medium5 of 10
- Topics
- Math, Combinatorics
- Solved
- No attempts yet
Problem
Jeongmin is a single-celled organism. The name JMPABU (JeongMin is Paramecium & Amoeba Based Unicellular organism) says as much: it looks like a paramecium crossed with an amoeba. I watched Jeongmin for a little over a year and found two properties.
- When a generation Jeongmin has a child, that child belongs to generation . A generation Jeongmin leaves exactly children in its life, then dies.
- Jeongmin carries an unusual gene. Depending on the order in which a Jeongmin is born, that gene can be the one that causes an inherited disease.
I own a single generation 0 Jeongmin, and I numbered it 0. Its descendants get numbers too. Take the smallest-numbered Jeongmin whose children are not numbered yet, then give its children the smallest unused numbers in birth order. Repeating that produces the numbering in the picture below.

I keep running experiments on which birth order leads to the disease. The method is simple: follow the birth orders one step at a time and examine the Jeongmin you land on. For example, look at the 1st child born to the generation 0 Jeongmin, then the 2nd child born to that one, then the 1st child born to that one. Along that path the numbers you see are 1, 3 and 7, in that order. Once the generation count grows the numbers get far too large to follow by hand. I need your help.
Input
The first line contains the generation () you want to examine.
The second line contains integers separated by spaces. The -th integer () means you look at the -th child born to the generation Jeongmin.
Output
Print lines. On line , print the number of the generation Jeongmin you see while following the input. The number can be very large, so print it modulo 1,000,000,007.