PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CC0000042. Đếm đoạn con có nhiều số 1 hơn số 0 (Count Subarrays With More Ones Than Zeros)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Đếm đoạn con có nhiều số 1 hơn số 0 (Count Subarrays With More Ones Than Zeros)

Count Subarrays With More Ones Than Zeros

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given a binary array, count contiguous subarrays containing strictly more ones than zeros. Print the result modulo 109+710^9+7.

Input

The first line is nn; the second line contains nn binary digits (space-separated).

Output

Print the answer modulo 109+710^9+7.

Subtasks

  • Subtask 1 (20 points): 1≤n≤151\le n\le 15; ai∈{0,1}a_i\in\{0,1\}.
  • Subtask 2 (30 points): 1≤n≤4201\le n\le 420; ai∈{0,1}a_i\in\{0,1\}.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; ai∈{0,1}a_i\in\{0,1\}.

Examples

Input

5
0 1 1 0 1

Output

9

Explanation

There are 3 valid subarrays of length 1, 1 of length 2, 3 of length 3, 1 of length 4 and 1 of length 5, totaling 9.