Sorting solution
Intuition\nIt quite obvious that this problem could be divided into two separate problems:\n\n1. Find the number of non overlapping ranges\n2. Find number of po
# Intuition\nIt quite obvious that this problem could be divided into two separate problems:\n\n1. Find the number of non overlapping ranges\n2. Find number of possible splits for known number of non overlapping ranges.\n\n\n# Approach\n**Find number of overlapping ranges**\nLet\u2019s sort array of ranges by their start and consider intervals one by one. Let compare next interval with previously considered. We can face two situations:\n1. **start[next] \u2264 end[previous]**\n![image.png](https://assets.leetcode.com/users/images/f89bbf03-0d93-47d8-87bb-55da6185517b_1678046102.2397377.png)\nNext interval overlap previous \u2014 we need merge intervals.How can we merge overlapping intervals? There could be two possible cases:\n1.1. **end[next] \u2265 end[previous]**\n![image.png](https://assets.leetcode.com/users/images/af53537e-c59a-4fad-8104-60bb6082fc3a_1678046200.0705585.png)\n1.2. **end[next]<end[previous]**\n![image.png](https://assets.leetcode.com/users/images/8823d2ce-b7c9-446f-b670-c2cefd91228c_1678046243.058007.png)\nTo get merged interval boundaries we can use the following formula: **start [merged]=start[previous]** intervals are sorted so start[previous] is always lower, **end[merged] = max(end[next], end[previous])** \u2014 we get the biggest of two ends for new interval. After merging we start compare next interval with merged one.\n2. **start[next] > end[previous]** next interval does not overlap previous one we just start using next interval as is to compare with next intervals.\n![image.png](https://assets.leetcode.com/users/images/8e904a8b-19dd-40f6-8f5d-b26cc0215196_1678046375.7832742.png)\n\n**Calculate number of ways to split ranges**\nWe calculated the number of not overlapping intervals but now we need calculate the number of possible splits. In fact we need to calculate the number of possible subsequences for merged array. The number of possible subsequences for an array of length N will be equal to 2^N.\n\n\n# Complexity\n- Time complexity:\nO(N*Log(N))\n\n- Space complexity:\nO(N)\n\n# Code\n```\n/**\n * @param {number[][]} ranges\n * @return {number}\n */\nvar countWays = function(ranges) {\n    let mod = Math.pow(10, 9) + 7\n    ranges.sort((r1, r2) => r1[0] - r2[0])\n    \n    let res = [ranges[0]]\n    for(let next of ranges){\n        let prev = res[res.length-1]\n        // Compare end of previous with start of next interval\n        if(prev[1] >= next[0]){\n            // Intervals overlap\n            prev[1] = Math.max(prev[1], next[1])\n        }else{\n            // Intervals not overlap\n            res.push(next)\n        }\n    }\n    \n    let n = res.length\n    \n    let sum = 1\n    for(let i = 1; i <= n; i++){\n        sum = (sum * 2) % mod\n    }\n    \n    return sum\n};\n```
['JavaScript']