<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <strong>positive</strong> integers.</p>

<p>A partition of an array into one or more <strong>contiguous</strong> subarrays is called <strong>good</strong> if no two subarrays contain the same number.</p>

<p>Return <em>the <strong>total number</strong> of good partitions of </em><code>nums</code>.</p>

<p>Since the answer may be 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,2,3,4]
<strong>Output:</strong> 8
<strong>Explanation:</strong> The 8 possible good partitions are: ([1], [2], [3], [4]), ([1], [2], [3,4]), ([1], [2,3], [4]), ([1], [2,3,4]), ([1,2], [3], [4]), ([1,2], [3,4]), ([1,2,3], [4]), and ([1,2,3,4]).
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,1,1]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The only possible good partition is: ([1,1,1,1]).
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,1,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The 2 possible good partitions are: ([1,2,1], [3]) and ([1,2,1,3]).
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def numberOfGoodPartitions(self, nums: List[int]) -> int:
        '''
            find distinct subarrays such that no two subarrays have no 2 same elements

             0  1. 2. 3. 4. 5. 6  7  8. 9.  10. 11  12  13. 14. 15. 16
            [1, 2, 4, 5, 1, 6, 7, 9, 5, 10, 12, 13, 14, 15, 13, 19, 20]

            [[0,4], [3, 8], [11, 14]]
            [[0,8], 9, 10, [11, 14], 15, 16] => 2^5 => 32

            [8]
            {
                1: 4
                2: 1
                4: 2
                5: 8
                6: 5
                7: 6
                9: 7
                10: 9
                12: 10
                13: 14
                14: 12
                15: 13
            }

            1. find intervals to be merged
            2. make it non overlapping
            3. count the number of distinct subarrays
                x         y
                |---------|
            x1.    y1
            |-------|.  y1
                    |---|
                     
                    x1          y1
                    |------------|
        '''

        hashMap = {}
        n = len(nums)
        toBeCollapsedIntervals = []
        for i in range(n):
            if nums[i] in hashMap:
                toBeCollapsedIntervals.append([hashMap[nums[i]], i])
            hashMap[nums[i]] = i

        modulo = int(1e9)+7
        # merge overlapping intervals
        if len(toBeCollapsedIntervals) == 0:
            return pow(2, n-1, modulo)
        result = []
        
        toBeCollapsedIntervals.sort(key=lambda x:x[0])
        result = [toBeCollapsedIntervals[0]]
        for i in range(1, len(toBeCollapsedIntervals)):
            currLow, currHigh = toBeCollapsedIntervals[i]
            prevLow, prevHigh = result[-1]
            if prevHigh >= currLow and currHigh >= prevLow:
                result.pop()
                result.append([min(currLow, prevLow), max(currHigh, prevHigh)])
            else:
                result.append(toBeCollapsedIntervals[i])
        
        count_distinct_subarrays = 0
        start = 0
        for i in range(len(result)):
            low, high = result[i]
            if low >= start:
                count_distinct_subarrays += low-start+1
                start = high+1
        while start < n:
            start+=1
            count_distinct_subarrays+=1
        
        return pow(2, count_distinct_subarrays-1, modulo)
831