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

#PS0000194. Đếm cách chia dãy thành ba đoạn (Ways to Split Array Into Three Subarrays)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đếm cách chia dãy thành ba đoạn (Ways to Split Array Into Three Subarrays)

Ways to Split Array Into Three Subarrays

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn nonnegative integers, choose two cuts 1≤i<j<n1\le i<j<n partitioning the entire array into three nonempty consecutive intervals [1,i][1,i], [i+1,j][i+1,j], [j+1,n][j+1,n]. A split is valid if the first interval sum is at most the second, and the second is at most the third. Count valid cut pairs modulo 109+710^9+7.

Input

The first line contains nn. The second contains nn integers aia_i.

Output

Print the number of valid splits modulo 109+710^9+7.

Subtasks

  • Subtask 1 (20 points): 3≤n≤5003\le n\le 500; 0≤ai≤1040\le a_i\le10^4.
  • Subtask 2 (30 points): 3≤n≤1000003\le n\le 100000; 0≤ai≤1040\le a_i\le10^4; ai=0a_i=0 for all ii.
  • Subtask 3 (50 points): 3≤n≤1000003\le n\le 100000; 0≤ai≤1040\le a_i\le10^4.

Example 1

Input

3
1 1 1

Output

1

Explanation

The only cut pair is (1,2)(1,2), with all sums equal to 1.

Example 2

Input

6
1 2 2 2 5 0

Output

3

Explanation

The valid cut pairs are (1,2)(1,2), (1,3)(1,3), (2,4)(2,4) with sums (1,2,9)(1,2,9), (1,4,7)(1,4,7), (3,4,5)(3,4,5).

Example 3

Input

3
3 2 1

Output

0

Explanation

The only split has sums 3,2,1, which are not ordered.

Example 4

Input

4
0 0 0 0

Output

3

Explanation

All three cut pairs have three zero sums.