<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:
        # a subarray must be inclusive of ALL of some number x
        # Doing this, we can reduce this to having some number of 'elements' that can be grouped freely
        # Suppose we know the number of elememnts, call it k
        # How many ways can we parition it?

        # 1 1
        # 2 2
        # 3 4
        # 4 8
        # 5 16
        # 6 32
        # so it seems our answer is 2 ^ (k-1)
        # The reason is:
        # Between every two numbers, you either do or do not split the interval

        # How to get k?
        # We can represent same number dependencies as interverals
        # We can then merge these intervals
        # and the number of distinct intervals remaining is our solution.

        minMaxes = defaultdict(lambda: [math.inf, -math.inf])
        for i, num in enumerate(nums):
            minMax = minMaxes[num]
            if i < minMax[0]:
                minMax[0] = i
            minMax[1] = i

        k = len(self.merge(list(minMaxes.values())))
        return (1 << (k - 1)) % int(1e9+7)

    # https://leetcode.com/problems/merge-intervals/submissions/1304028875/
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:
        intervals.sort()    # sort by interval start (sort implicitly does this, and then breaks ties by second element)

        nonOverlapping = [intervals[0]]
        for start, end in intervals[1:]:
            if start <= nonOverlapping[-1][1]:
                nonOverlapping[-1][1] = max(nonOverlapping[-1][1], end)
            else:
                nonOverlapping.append([start, end])

        return nonOverlapping
873