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

#FT0000102. Phân nhóm đàn bò biểu tình (Generic Cow Protests)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Phân nhóm đàn bò biểu tình (Generic Cow Protests)

Generic Cow Protests

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

Farmer John has nn cows standing in a line, numbered 11 through nn. Cow ii has an integer rationality level aia_i, which may be negative, zero, or positive.

John wants to partition the entire line into one or more nonempty consecutive groups. Every cow must belong to exactly one group. A partition is valid if the sum of rationality levels within every group is nonnegative.

Count all ways to place boundaries between cows such that the resulting partition is valid. Different boundary sets count as different partitions. Print the count modulo 109+910^9+9.

Input

  • The first line contains integer nn, the number of cows.
  • Each of the next nn lines contains integer aia_i, the rationality level of cow ii in order.

Output

Print one integer: the number of valid partitions modulo 109+910^9+9.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le 20; −104≤ai≤104-10^4\le a_i\le 10^4 for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; −104≤ai≤104-10^4\le a_i\le 10^4 for every 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; −104≤ai≤104-10^4\le a_i\le 10^4 for every 1≤i≤n1\le i\le n.

Examples

Example 1

Input

4
2
3
-3
1

Output

4

Explanation

The rationality levels are (2,3,−3,1)(2,3,-3,1). The four valid partitions are (2,3,−3,1)(2,3,-3,1) with sum 33; (2,3,−3)∣(1)(2,3,-3)\mid(1) with sums 2,12,1; (2)∣(3,−3,1)(2)\mid(3,-3,1) with sums 2,12,1; and (2)∣(3,−3)∣(1)(2)\mid(3,-3)\mid(1) with sums 2,0,12,0,1. Every group's sum is nonnegative. All other boundary choices create a negative-sum group, so print 4.

Example 2

Input

1
-1

Output

0

Explanation

There is one cow with level −1-1. The only partition is the single group (−1)(-1), whose sum is negative. No valid partition exists, so print 0.