CF1025G.Company Acquisitions

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are nn 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.

  1. Two distinct active startups AA , BB , are chosen uniformly at random.
  2. A fair coin is flipped, and with equal probability, AA acquires BB or BB acquires AA (i.e. if AA acquires BB , then that means BB 's state changes from active to acquired, and its starts following AA ).
  3. 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 AA , BB are active startups. CC , DD , EE are acquired startups under AA , and FF , GG are acquired startups under BB :

Active startups are shown in red.

If AA acquires BB , then the state will be AA , FF , GG are active startups. CC , DD , EE , BB are acquired startups under AA . FF and GG have no acquired startups:

If instead, BB acquires AA , then the state will be BB , CC , DD , EE are active startups. FF , GG , AA are acquired startups under BB . CC , DD , EE 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 P/QP/Q , where PP and QQ are co-prime integers, and Q0(mod109+7)Q \not= 0 \pmod{10^9+7} . Return the value of PQ1P \cdot Q^{-1} modulo 109+710^9+7 .

输入格式

The first line contains a single integer nn ( 2n5002 \leq n \leq 500 ), the number of startups.

The next line will contain nn space-separated integers a1,a2,,ana_1, a_2, \ldots, a_n ( ai=1a_i = -1 or 1ain1 \leq a_i \leq n ). If ai=1a_i = -1 , then that means startup ii is active. Otherwise, if 1ain1 \leq a_i \leq n , then startup ii is acquired, and it is currently following startup aia_i . It is guaranteed if ai1a_i \not= -1 , then aai=1a_{a_i} =-1 (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 109+710^9+7 .

输入输出样例

  • 输入#1

    3
    -1 -1 -1
    

    输出#1

    3
    
  • 输入#2

    2
    2 -1
    

    输出#2

    0
    
  • 输入#3

    40
    3 3 -1 -1 4 4 -1 -1 -1 -1 -1 10 10 10 10 10 10 4 20 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 3 3 3 3 3 3 3
    

    输出#3

    755808950
    

说明/提示

In the first sample, there are three active startups labeled 11 , 22 and 33 , and zero acquired startups. Here's an example of how one scenario can happen

  1. Startup 11 acquires startup 22 (This state can be represented by the array [1,1,1][-1, 1, -1] )
  2. Startup 33 acquires startup 11 (This state can be represented by the array [3,1,1][3, -1, -1] )
  3. Startup 22 acquires startup 33 (This state can be represented by the array [1,1,2][-1, -1, 2] ).
  4. Startup 22 acquires startup 11 (This state can be represented by the array [2,1,2][2, -1, 2] ).

At this point, there is only one active startup, and this sequence of steps took 44 days. It can be shown the expected number of days is 33 .

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 109+710^9+7 .

首页