<p>You are given an integer array <code>nums</code>. We call a subset of <code>nums</code> <strong>good</strong> if its product can be represented as a product of one or more <strong>distinct prime</strong> numbers.</p>

<ul>
	<li>For example, if <code>nums = [1, 2, 3, 4]</code>:

	<ul>
		<li><code>[2, 3]</code>, <code>[1, 2, 3]</code>, and <code>[1, 3]</code> are <strong>good</strong> subsets with products <code>6 = 2*3</code>, <code>6 = 2*3</code>, and <code>3 = 3</code> respectively.</li>
		<li><code>[1, 4]</code> and <code>[4]</code> are not <strong>good</strong> subsets with products <code>4 = 2*2</code> and <code>4 = 2*2</code> respectively.</li>
	</ul>
	</li>
</ul>

<p>Return <em>the number of different <strong>good</strong> subsets in </em><code>nums</code><em> <strong>modulo</strong> </em><code>10<sup>9</sup> + 7</code>.</p>

<p>A <strong>subset</strong> of <code>nums</code> is any array that can be obtained by deleting some (possibly none or all) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The good subsets are:
- [1,2]: product is 2, which is the product of distinct prime 2.
- [1,2,3]: product is 6, which is the product of distinct primes 2 and 3.
- [1,3]: product is 3, which is the product of distinct prime 3.
- [2]: product is 2, which is the product of distinct prime 2.
- [2,3]: product is 6, which is the product of distinct primes 2 and 3.
- [3]: product is 3, which is the product of distinct prime 3.
</pre>

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

<pre>
<strong>Input:</strong> nums = [4,2,3,15]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good subsets are:
- [2]: product is 2, which is the product of distinct prime 2.
- [2,3]: product is 6, which is the product of distinct primes 2 and 3.
- [2,15]: product is 30, which is the product of distinct primes 2, 3, and 5.
- [3]: product is 3, which is the product of distinct prime 3.
- [15]: product is 15, which is the product of distinct primes 3 and 5.
</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;= 30</code></li>
</ul>

class Solution:
    def numberOfGoodSubsets(self, nums: List[int]) -> int:
        # subsets
        #   good if it's a product of 1+ distinct primes
        #   1 is not prime
        
        # return the number of good subsets in nums mod BIG

        # good subsets: only one of each prime factor

        # different iff the chosen indices are different

        # some edge cases: all ones: any combination of ones in addition to the other numbers is acceptable
        
        # if we take something with factors e.g. 2, 3, 5
        #   then only numbers with factors 7, 11, 13, .. are allowed

        BIG = 10**9 + 7
        M = max(nums)

        ones = sum(n == 1 for n in nums)

        @cache
        def distinctPrimes(x: int) -> bool:
            r = x
            for d in chain([2], range(3, int(sqrt(x))+1, 2)):
                if r % d**2 == 0:
                    return 0 # two factors of prime d
                elif r % d == 0:
                    r //= d

                if r == 1: break

            # there is at most one prime factor of x greater than sqrt(x) for all x
            return True

        candidates = Counter(n for n in nums if n > 1 and distinctPrimes(n))
        candidates = list(candidates.items())
        C = len(candidates)

        # for numbers >= 2 we can
        #    > if the current product and current number have no common factors
        #       then we can pick any copy of the current number and proceed with the product
        #    > we can always not include the number
        # there is one way to pick none of the numbers so we'll take all possible picks and subtract one

        @cache
        def ways(i: int, product: int) -> int:
            if i == C: return 1

            v, f = candidates[i]

            ans = ways(i+1, product) # any later combination given product and take zero copies of this number
            
            if gcd(product, v) == 1:
                ans += f * ways(i+1, v*product)

            return ans % BIG

        total = ways(0, 1) - 1 # all subsets of values >= 2, minus one to avoid counting the empty set

        # can take any number of ones as well
        total *= pow(2, ones, BIG)

        return total % BIG        
1600