Consecutive Classes Are Tough (Hard)
Time limit1sMemory limit1024 MB
Given n periods where some classes are required, count the 0/1 timetables whose longest run of consecutive classes is exactly k, modulo 1e9+7.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Prefix sum, String
- Solved
- No attempts yet
Problem
This problem is the harder version of Consecutive Classes Are Tough (Easy), and you can get it accepted by submitting the correct code for this problem to Consecutive Classes Are Tough (Easy).
Dohoon failed to register for courses last semester.
A school day has periods , and distinct classes each occupy one distinct period. In this registration, Dohoon wants to pick classes so that the longest run of consecutive classes is exactly periods long. Several runs of consecutive periods may appear.

However, some required classes must be taken. Consider the required classes and help Dohoon with his registration!
Input
The first line contains two integers and , separated by a space.
The second line contains integers , separated by spaces. is if the class in period is required, and otherwise.
Output
Print the number of timetables in which the longest run of consecutive classes is exactly periods, modulo .
Constraints
- .
- .