Java#MaxHeap#FastModularExp
Approach\nmodExp implemented Modular_exponentiation\n\n# Complexity\n- Time complexity:\nSorting: O(NlogN).\nPriorityQueue: O(NLogN) the Worst Case scenario.\nM
# Approach\n`modExp` implemented [Modular_exponentiation](https://en.wikipedia.org/wiki/Modular_exponentiation)\n\n# Complexity\n- Time complexity:\n**Sorting**: `O(NlogN)`.\n**PriorityQueue**: `O(NLogN)` the Worst Case scenario.\n**Modular Exponentiation**: The `modExp` function has a time complexity of  `O(LogM)`, `M` is size of `PriorityQueue`, in the Worst Case scenario is `N` => `O(LogN)`.\n\n- Space complexity:\nA size of `PriorityQueue` is `M`, `O(M)`, in the Worst Case scenario is `N` => `O(LogN)`.\n\n# Code\n```java []\n\nclass Solution {\n    public int countWays(int[][] ranges) {\n        Arrays.sort(ranges, Comparator.comparingInt(a -> a[0]));\n\n        Queue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> -a[1]));\n        pq.offer(ranges[0]);\n\n        for (int i = 1; i < ranges.length; i++) {\n            int[] lastRange = pq.peek();\n            if (lastRange[1] >= ranges[i][0]) {\n                lastRange = pq.poll();\n                lastRange[1] = Math.max(lastRange[1], ranges[i][1]);\n                pq.offer(lastRange);\n            } else {\n                pq.offer(ranges[i]);\n            }\n        }\n\n        return (int) modExp(2, pq.size(), 1000000007);\n    }\n\n    private static long modExp(long base, long exp, long mod) {\n        long result = 1;\n        base = base % mod;\n\n        while (exp > 0) {\n            if ((exp & 1) == 1) {\n                result = (result * base) % mod;\n            }\n            exp = exp >> 1;\n            base = (base * base) % mod;\n        }\n        return result;\n    }\n}\n```
['Array', 'Heap (Priority Queue)', 'Java']