[Javascript] Binary search twice O(NlgN)
For each i:\n  First binary search find the smallest possible ending idx of mid arr: j\n  Second binary search find the largest possible ending idx of mid arr: 
For each i:\n*  First binary search find the smallest possible ending idx of mid arr: j\n*  Second binary search find the largest possible ending idx of mid arr: k\n\n\n```\nvar waysToSplit = function (A) {\n  const n = A.length;\n  const mod = 1e9 + 7;\n  const pre = [...A];\n  for (let i = 1; i < n; i++) pre[i] += pre[i - 1];\n  let res = 0;\n  for (let i = 0; i < n; i++) {\n    const left = pre[i];\n    if (left > pre[n - 1] / 3) break;\n\n    // First binary search find the smallest possible ending idx of mid arr: j\n    let [l, r] = [i + 1, n - 2];\n    while (l < r) {\n      const mid = (l + r) >> 1;\n      const midV = pre[mid] - pre[i];\n      if (midV < left) l = mid + 1;\n      else r = mid;\n    }\n    // Double check\n    if (pre[l] - pre[i] < left) continue;\n    const j = l;\n\n    // Second binary search find the largest possible ending idx of mid arr: k\n    r = n - 2;\n    while (l < r) {\n      const mid = (l + r) >> 1;\n      const midV = pre[mid] - pre[i];\n      const rightV = pre[n - 1] - pre[mid];\n      if (midV <= rightV) l = mid + 1;\n      else r = mid - 1;\n    }\n    \n    // Double check\n    if (pre[l] - pre[i] > pre[n - 1] - pre[l] || l >= n - 1) {\n      l--;\n      if (pre[l] - pre[i] > pre[n - 1] - pre[l] || l < j) continue;\n    }\n    const k = l;\n\n    res += (k - j + 1) % mod;\n    res %= mod;\n  }\n  return res;\n};\n```
['Binary Search', 'JavaScript']