<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>

from bisect import bisect_left, bisect_right
from itertools import accumulate

MOD = 10**9 + 7


class Solution:
    def waysToSplit(self, nums: list[int]) -> int:
        n, ans = len(nums), 0
        ps = [0] + [*accumulate(nums)]

        for cut_1 in range(1, n):
            sum_1 = ps[cut_1]  # sum(nums[:cut_1])
            if sum_1 * 3 > ps[-1]:
                break

            # bisect for the minimum and maximum cut_2
            # here min_2 and max_2 for a larger cut_1
            min_2 = bisect_left(ps, sum_1 * 2, cut_1 + 1)
            # max_2 is the last index,
            # where           sum(nums[cut_1:cut_2]) <= sum(nums[cut_2:])
            # that's saying    total - sum_1 - sum_3 <= sum_3
            # or meanly                total - sum_1 <= 2*sum_3
            # I was wrong to say lhs//2 <= sum_3, like case lhs,sum_3 = 3,2.
            # it should be lhs/2 <= sum_3, meanly, ceil_div(lhs, 2) <= sum_3
            # now reduce to max_2, it should be the last index where
            # (ps[-1] - sum_1) / 2 <= ps[-1] - ps[cut_2]
            # ps[cut_2] <= ps[-1] - (ps[-1] - sum_1) / 2
            # or the first index that
            # ps[cut_2] > ps[-1] - (ps[-1] - sum_1) / 2
            max_2 = bisect_right(ps, ps[-1] - (ps[-1] - sum_1) / 2, cut_1 + 1)
            if max_2 < min_2:
                continue
            elif max_2 == n + 1:  # not possible to cut after last element
                ans += max_2 - min_2 - 1
            else:
                ans += max_2 - min_2
        return ans % MOD


783