Under construction

Still being built. Things may change

Consecutive Classes Are Tough (Hard)

Time limit1sMemory limit1024 MB

Summary
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 1,2,…,n1,2,\ldots,n, and nn 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 kk periods long. Several runs of kk 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 nn and kk, separated by a space.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n, separated by spaces. aia_i is 11 if the class in period ii is required, and 00 otherwise.

Output

Print the number of timetables in which the longest run of consecutive classes is exactly kk periods, modulo 109+710^9+7.

Constraints

  • 1≤k≤n≤1061\le k\le n\le 10^6.
  • ai∈{0,1}a_i\in\{0,1\}.

Examples2

  1. Example 1

    Input
    5 2
    0 0 0 0 0
    
    Expected output
    11
    
  2. Example 2

    Input
    5 2
    0 1 0 0 1
    
    Expected output
    4