<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:
        i = 0
        j = 0
        k = 0
        mod = (10 ** 9) + 7
        result = 0

        # Things to remember, 
        # i will be pointing to index of end of the left subarray
        # j will be pointing to index of start of the mid subarray
        # k will be pointing to index of end of the right subarray

        # Now, How to get subarray sum for left, mid and right subarray,
        # we can create prefix sum array

        prefixSum = nums.copy()
        for i in range(1, len(nums)):
            prefixSum[i] += prefixSum[i - 1]

        # Now how to get left subarray sum,
        # Simple, since i points to end of left subarray
        # left subarray sum = prefixSum[i]

        # Now, how to get mid subarray sum
        # mid subarray sum = prefixSum[i] - left subarray sum
        
        # How, to get right subarray sum
        # right subarray sum = prefixSum[-1] - prefixSum[k], since k points to end of mid subarray

        # Whole purpose is to expand left subarray and mid subarray
        for i in range(len(prefixSum)):
            # left subarray sum
            left = prefixSum[i]
            # Where to start, j, either from its current position but it must be greater than i, as it is start of mid subarray
            j = max(i + 1, j)
            # Now, we will increase j pointer, until we get a mid subarray sum greater than left subarray sum
            # Logic:? j points to start of the mid, leftSum <= midSum
            # prefixSum[j] - left(left subarray sum) should be less than leftSum
            while j < len(prefixSum) and prefixSum[j] - left < left:
                j += 1
            # No num left for right
            if j >= len(prefixSum):
                break
            # Need to find right subarray sum
            # Logic, Since, k is the end of mid subarray,
            # right subarray sum should be greater than mid subarray sum
            # right > mid
            # prefixSum[-1] - prefixSum[k] >= prefixSum[k] - prefixSum[i]
            k = max(j, k)
            while k < len(prefixSum) - 1 and prefixSum[-1] - prefixSum[k] >= prefixSum[k] - prefixSum[i]:
                k += 1
            # What will be the result, Number of mid subarrays we can create
            # So, it will be number of elements in midSubarray, i,e end point of midSubarray (k) - start point of the midSubarray
            # Number of possible middle subarrays is k - j
            result = (result + k - j) % mod
        return result
835