<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:
    N = int(1e9) + 7

    # Function to perform binary exponentiation
    def binary(self, a, b):
        res = 1
        a = a
        while b > 0:
            if b & 1 == 1:
                res = ((res % self.N) * (a % self.N)) % self.N
            a = ((a % self.N) * (a % self.N)) % self.N
            res = res % self.N
            b >>= 1
        return res

    # Function to calculate the number of good partitions
    def numberOfGoodPartitions(self, nums: List[int]) -> int:
        s = set()  # Set to store frequency and element
        m = {}  # Dictionary to store frequency of elements

        # Count frequency of each element in the nums array
        for x in nums:
            m[x] = m.get(x, 0) + 1

        count = 0
        i = 0
        n = len(nums)

        # Iterate through the array
        while i < n:
            s.discard((-1 * m[nums[i]], nums[i]))  # Remove frequency and element pair from set
            m[nums[i]] -= 1  # Decrease frequency of the current element

            # If the element still has frequency left, insert it back into the set
            if m[nums[i]] > 0:
                s.add((-1 * m[nums[i]], nums[i]))

            i += 1

            # If the set is empty, increment the count
            if len(s) == 0:
                count += 1

        # Calculate the result using binary exponentiation
        return self.binary(2, count - 1)
        # Equivalent way to calculate result using left shift operator
        # return (1 << (count - 1))
873