Under construction

Still being built. Things may change

Turn Off The Light

Time limit2sMemory limit256 MB

Summary
For each start position p, find the fewest steps needed to turn off all lights, then print the sum of p times that minimum, modulo 1e9+7.
Level

Hard8 of 10

Topics
Dynamic programming, Greedy, Prefix sum
Solved
No attempts yet

Problem

There are nn lights in a row, numbered 11 to nn from left to right. Some of the lights are on at the start. Chiaki wants to turn off all the lights.

Chiaki starts at the pp-th light. Each step, she moves one position to the left or to the right, then presses the switch of the light at her new position. Pressing a switch turns a lit light off and an unlit light on.

For each p=1,2,…,np=1,2,\dots,n, find the minimum number of steps needed to turn off all the lights.

Input

The first line contains an integer TT, the number of test cases. Each test case is given as follows.

The first line contains an integer nn (2≤n≤1062 \le n \le 10^6), the number of lights.

The second line contains a binary string ss. If si=1s_i=1, the ii-th light is on. If si=0s_i=0, the ii-th light is off.

The sum of nn over all test cases does not exceed 10710^7.

Output

For each test case, print (∑i=1∣s∣i×zi) mod (109+7)\left(\sum_{i=1}^{|s|} i \times z_i\right) \bmod (10^9+7), where ziz_i is the minimum number of steps needed when Chiaki starts at the ii-th light.

Examples1

  1. Example 1

    Input
    3
    3
    000
    3
    111
    8
    01010101
    
    Expected output
    0
    26
    432