Gomory și scândurile de lemn

Time limit: 0.25s Memory limit: 32MB Input: Output:

Gomory has just moved to the Lucky Mill. He cares a lot about the aesthetics of the fence surrounding the mill, but some planks are missing, and the remaining ones differ in height. For economic reasons, he will leave the planks that already exist in place and will only add new ones in the missing positions. Also, so that the mill does not appear too confusing to passersby, he wants a certain height to be a majority. Since Gomory is too busy counting his money, help him by counting the ways in which he can build the fence.

Task

Formally, you are given an array hh of length nn representing the heights of the planks. If a plank is missing at position ii, then you will receive the value 1-1. Otherwise, you will receive a natural number between 11 and nn representing the height of the plank at position ii. You must output the number of ways to replace the values 1-1 in the array with natural numbers between 11 and nn such that there exists a majority element.

Input Data

On the first line, a natural number nn is read, representing the number of planks in the fence.

On the second line, the elements of the array hh are read, separated by a space.

Output Data

You must output a single number representing the number of ways modulo 109+710^9 + 7 in which a majority element can be obtained.

Constraints and Notes

  • 1n200,0001 \le n \le 200{,}000
  • hi=1h_i = -1 or 1hin1 \le h_i \le n, for any ii from 11 to nn
  • An element is majority if it appears at least n2+1\lfloor\frac{n}{2}\rfloor + 1 times.
# Score Constraints
1 15 1n81 \leq n \leq 8
2 15 The fence initially contains no planks (the array hh contains only the value 1-1)
3 30 n2000n \le 2\,000
4 40 No additional constraints

Example

stdin

4
1 -1 2 -1

stdout

2

Explanation

The values equal to 1-1 can be replaced with any value from 11 to 44. The only possibilities with a majority element are: [1,1,2,1][1 , 1 , 2 , 1] and [1,2,2,2][1 , 2 , 2 , 2]. If we form the array [1,2,2,3][1 , 2 , 2 , 3], 22 is not a majority element because it appears fewer than 42+1\lfloor\frac{4}{2}\rfloor + 1 times, that is, fewer than 33 times.

Log in or sign up to be able to send submissions!