#P1025G. Company Acquisitions
Company Acquisitions
Description
There are startups. Startups can be active or acquired. If a startup is acquired, then that means it has exactly one active startup that it is following. An active startup can have arbitrarily many acquired startups that are following it. An active startup cannot follow any other startup.
The following steps happen until there is exactly one active startup. The following sequence of steps takes exactly 1 day.
- Two distinct active startups , , are chosen uniformly at random.
- A fair coin is flipped, and with equal probability, acquires or acquires (i.e. if acquires , then that means 's state changes from active to acquired, and its starts following ).
- When a startup changes from active to acquired, all of its previously acquired startups become active.
For example, the following scenario can happen: Let's say , are active startups. , , are acquired startups under , and , are acquired startups under :

Active startups are shown in red.
If acquires , then the state will be , , are active startups. , , , are acquired startups under . and have no acquired startups:

If instead, acquires , then the state will be , , , are active startups. , , are acquired startups under . , , have no acquired startups:

You are given the initial state of the startups. For each startup, you are told if it is either acquired or active. If it is acquired, you are also given the index of the active startup that it is following.
You're now wondering, what is the expected number of days needed for this process to finish with exactly one active startup at the end.
It can be shown the expected number of days can be written as a rational number , where and are co-prime integers, and . Return the value of modulo .
The first line contains a single integer (), the number of startups.
The next line will contain space-separated integers ( or ). If , then that means startup is active. Otherwise, if , then startup is acquired, and it is currently following startup . It is guaranteed if , then (that is, all startups that are being followed are active).
Print a single integer, the expected number of days needed for the process to end with exactly one active startup, modulo .
Input
The first line contains a single integer (), the number of startups.
The next line will contain space-separated integers ( or ). If , then that means startup is active. Otherwise, if , then startup is acquired, and it is currently following startup . It is guaranteed if , then (that is, all startups that are being followed are active).
Output
Print a single integer, the expected number of days needed for the process to end with exactly one active startup, modulo .
Note
In the first sample, there are three active startups labeled , and , and zero acquired startups. Here's an example of how one scenario can happen
- Startup acquires startup (This state can be represented by the array )
- Startup acquires startup (This state can be represented by the array )
- Startup acquires startup (This state can be represented by the array ).
- Startup acquires startup (This state can be represented by the array ).
At this point, there is only one active startup, and this sequence of steps took days. It can be shown the expected number of days is .
For the second sample, there is only one active startup, so we need zero days.
For the last sample, remember to take the answer modulo .