<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 {
public:
// [1,2,2,2,5,0]
// [1,3,5,7,12,12]
    const int MOD = 1e9 + 7;

    int waysToSplit(vector<int>& nums) {
        int n = nums.size();
        vector<long long> prefixSum(n + 1, 0);

        // Compute prefix sum
        for (int i = 1; i <= n; ++i) {
            prefixSum[i] = prefixSum[i - 1] + nums[i - 1];
        }

        int count = 0;
        
        // Iterate over possible endings for the left subarray
        for (int i = 1; i <= n - 2; ++i) {
            // Binary search for the minimum j (start of mid) such that left sum <= mid sum
            int left = i + 1, right = n - 1, mid_start = n;
            while (left <= right) {
                int mid = (left + right) / 2;
                if (prefixSum[mid] - prefixSum[i] >= prefixSum[i]) {
                    mid_start = mid;
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            }

            // Binary search for the maximum j (start of mid) such that mid sum <= right sum
            left = i + 1, right = n - 1;
            int mid_end = i;
            while (left <= right) {
                int mid = (left + right) / 2;
                if (prefixSum[n] - prefixSum[mid] >= prefixSum[mid] - prefixSum[i]) {
                    mid_end = mid;
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }

            // Add the number of valid mid subarrays
            if (mid_start <= mid_end) {
                count = (count + (mid_end - mid_start + 1)) % MOD;
            }
        }

        return count;
    }
};
91300