There are $n$ 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 $A$, $B$, are chosen uniformly at random.  A fair coin is flipped, and with equal probability, $A$ acquires $B$ or $B$ acquires $A$ (i.e. if $A$ acquires $B$, then that means $B$'s state changes from active to acquired, and its starts following $A$).  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 $A$, $B$ are active startups. $C$, $D$, $E$ are acquired startups under $A$, and $F$, $G$ are acquired startups under $B$: [Image]

Active startups are shown in red. 

If $A$ acquires $B$, then the state will be $A$, $F$, $G$ are active startups. $C$, $D$, $E$, $B$ are acquired startups under $A$. $F$ and $G$ have no acquired startups: $G$ 

If instead, $B$ acquires $A$, then the state will be $B$, $C$, $D$, $E$ are active startups. $F$, $G$, $A$ are acquired startups under $B$. $C$, $D$, $E$ have no acquired startups: [Image] 

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/Q$, where $P$ and $Q$ are co-prime integers, and $Q \not= 0 \pmod{10^9+7}$. Return the value of $P \cdot Q^{-1}$ modulo $10^9+7$.


-----Input-----

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

The next line will contain $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($a_i = -1$ or $1 \leq a_i \leq n$). If $a_i = -1$, then that means startup $i$ is active. Otherwise, if $1 \leq a_i \leq n$, then startup $i$ is acquired, and it is currently following startup $a_i$. It is guaranteed if $a_i \not= -1$, then $a_{a_i} =-1$ (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 $10^9+7$.


-----Examples-----
Input
3
-1 -1 -1

Output
3

Input
2
2 -1

Output
0

Input
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

Output
755808950



-----Note-----

In the first sample, there are three active startups labeled $1$, $2$ and $3$, and zero acquired startups. Here's an example of how one scenario can happen  Startup $1$ acquires startup $2$ (This state can be represented by the array $[-1, 1, -1]$)  Startup $3$ acquires startup $1$ (This state can be represented by the array $[3, -1, -1]$)  Startup $2$ acquires startup $3$ (This state can be represented by the array $[-1, -1, 2]$).  Startup $2$ acquires startup $1$ (This state can be represented by the array $[2, -1, 2]$). 

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

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 $10^9+7$.

Source: codeforces | Difficulty: VERY_HARD
Tags: Mathematics

Constructive algorithms


Solutions:
(m, n, a) = (1000000007, int(input()), input().split())
print(pow(2, n - 1, m) - 1 - sum((pow(2, a.count(x), m) - 1 for x in set(a) if x != '-1')) % m)


n = int(input())
a = [int(i) for i in input().split()]
b = [0] * (n + 1)
sum = 2 ** (n - 1) - 1
for i in a:
	if i > 0:
		b[i] = b[i] + 1
for i in b:
	sum = sum - 2 ** i + 1
print(sum % 1000000007)


(n, a) = (int(input()), input().split())
print((pow(2, n - 1) - 1 - sum((pow(2, a.count(x)) - 1 for x in {*a} - {'-1'}))) % (10 ** 9 + 7))


from fractions import Fraction
from copy import copy
from random import randint
from collections import *
import sys
sys.setrecursionlimit(1500)

class F:

	def __init__(self, m, n):
		self.m = m
		self.n = n
		self.save = {}

	def __call__(self, a):
		if a in self.save:
			return self.save[a]
		if type(a) != Fraction:
			a = Fraction(a)
		m = self.m
		n = self.n
		if a == 0:
			return Fraction(-0.5)
		if a == 1:
			return Fraction(-1)
		self.save[a] = 2 * f(a - 1) - (a - 2) * f(1) - 1
		return self.save[a]

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, y, x) = egcd(b % a, a)
		return (g, x - b // a * y, y)

def modinv(a, m):
	(g, x, y) = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m
n = int(input())
arr = [int(x) for x in input().split()]
a = []
cnt = defaultdict(int)
for x in arr:
	cnt[x] += 1
for key in cnt:
	cnt[-1] -= 1
cnt[-1] += 1
for (key, value) in cnt.items():
	if key == -1:
		for i in range(value):
			a.append(1)
	else:
		a.append(value + 1)
n = len(a)
m = sum(a)
f = F(m, n)
ans = sum((f(x) for x in a)) - f(m)
MOD = 10 ** 9 + 7
print(ans.numerator * modinv(ans.denominator, MOD) % MOD)


input()
a = input().split()
print((pow(2, len(a) - 1) - 1 - sum((pow(2, a.count(x)) - 1 for x in {*a} - {'-1'}))) % (10 ** 9 + 7))


n = int(input())
r = list(map(int, input().split()))
f = [0] * 501
for t in r:
	if t >= 0:
		f[t] += 1
print((2 ** (n - 1) - 1 - sum((2 ** k - 1 for k in f))) % 1000000007)


mod = 10 ** 9 + 7
n = int(input())
a = input().split()
ans = pow(2, n - 1, mod) - 1
sz = [0] * (n + 1)
for i in a:
	i = int(i)
	if i != -1:
		sz[i] += 1
for i in range(1, n + 1):
	ans -= pow(2, sz[i], mod) - 1
print((ans % mod + mod) % mod)