<p>A split of an integer array is <strong>good</strong> if:</p>

<ul>
	<li>The array is split into three <strong>non-empty</strong> contiguous subarrays - named <code>left</code>, <code>mid</code>, <code>right</code> respectively from left to right.</li>
	<li>The sum of the elements in <code>left</code> is less than or equal to the sum of the elements in <code>mid</code>, and the sum of the elements in <code>mid</code> is less than or equal to the sum of the elements in <code>right</code>.</li>
</ul>

<p>Given <code>nums</code>, an array of <strong>non-negative</strong> integers, return <em>the number of <strong>good</strong> ways to split</em> <code>nums</code>. As the number may be too large, return it <strong>modulo</strong> <code>10<sup>9 </sup>+ 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The only good way to split nums is [1] [1] [1].</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,2,2,5,0]
<strong>Output:</strong> 3
<strong>Explanation:</strong> There are three good ways of splitting nums:
[1] [2] [2,2,5,0]
[1] [2,2] [2,5,0]
[1,2] [2,2] [5,0]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,2,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no good way to split nums.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution:
    def waysToSplit(self, nums: List[int]) -> int:
        MOD = int(1e9 + 7)
        N = len(nums)
        
        # prefix array
        A = [0] * N
        A[0] = nums[0]
        for i in range(1, N):
            A[i] = A[i - 1] + nums[i]
        
        res = 0
        for i in range(1, N - 1):
            if A[i - 1] > (A[N - 1] - A[i - 1]) / 2:
                break  # early termination
            
            left = self.helper(A, A[i - 1], i, True)
            right = self.helper(A, A[i - 1], i, False)
            print(i, left, right)
            if left == -1 or right == -1:
                continue  # none is satisfied
            
            res = (res + (right - left + 1) % MOD) % MOD
        
        return res
    
    def helper(self, A: List[int], left_sum: int, index: int, search_left: bool) -> int:
        N = len(A)
        l, r = index, N - 2
        res = -1
        
        while l <= r:
            m = (r + l) // 2
            mid_sum = A[m] - A[index - 1]
            right_sum = A[N - 1] - A[m]
            
            if left_sum <= mid_sum <= right_sum:
                res = m
                if search_left:
                    r = m - 1
                else:
                    l = m + 1
            elif left_sum > mid_sum:  # shrink left
                l = m + 1
            else:  # shrink right
                r = m - 1
        
        return res

1662