<p>You are given a positive integer <strong>0-indexed</strong>&nbsp;array <code>nums</code>.</p>

<p>A subset of the array <code>nums</code> is <strong>square-free</strong> if the product of its elements is a <strong>square-free integer</strong>.</p>

<p>A <strong>square-free integer</strong> is an integer that is divisible by no square number other than <code>1</code>.</p>

<p>Return <em>the number of square-free non-empty subsets of the array</em> <strong>nums</strong>. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>A <strong>non-empty</strong>&nbsp;<strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none but not 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 = [3,4,4,5]
<strong>Output:</strong> 3
<strong>Explanation:</strong> There are 3 square-free subsets in this example:
- The subset consisting of the 0<sup>th</sup> element [3]. The product of its elements is 3, which is a square-free integer.
- The subset consisting of the 3<sup>rd</sup> element [5]. The product of its elements is 5, which is a square-free integer.
- The subset consisting of 0<sup>th</sup> and 3<sup>rd</sup> elements [3,5]. The product of its elements is 15, which is a square-free integer.
It can be proven that there are no more than 3 square-free subsets in the given array.</pre>

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

<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is 1 square-free subset in this example:
- The subset consisting of the 0<sup>th</sup> element [1]. The product of its elements is 1, which is a square-free integer.
It can be proven that there is no more than 1 square-free subset in the given array.
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length&nbsp;&lt;= 1000</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 30</code></li>
</ul>

from typing import List
from collections import defaultdict

class Solution:
    def squareFreeSubsets(self, nums: List[int]) -> int:
        MOD = 10**9 + 7
        
        # Precompute the square-free status of numbers from 1 to 30
        def is_square_free(n):
            for i in range(2, int(n**0.5) + 1):
                if n % (i * i) == 0:
                    return False
            return True
        
        # Precompute the square-free mask for numbers from 1 to 30
        # The mask is a bitmask representation of the prime factors
        def square_free_mask(n):
            mask = 0
            for i in range(2, 31):
                if n % i == 0:
                    if n % (i * i) == 0:
                        return None  # Not square-free
                    mask |= (1 << (i - 2))
            return mask
        
        # Precompute masks
        masks = [square_free_mask(i) for i in range(31)]
        
        # DP table to store the number of ways to form square-free subsets
        dp = defaultdict(int)
        dp[0] = 1  # Base case: one way to form an empty subset
        
        for num in nums:
            mask = masks[num]
            if mask is None:
                continue  # Skip non-square-free numbers
            
            # Update dp table in reverse order to avoid overwriting
            for existing_mask in list(dp.keys()):
                new_mask = existing_mask | mask
                if existing_mask & mask == 0:  # Ensure no common prime factors
                    dp[new_mask] = (dp[new_mask] + dp[existing_mask]) % MOD
        
        # Sum all non-empty subsets
        result = (sum(dp.values()) - 1) % MOD  # Subtract the empty subset
        
        return result

# Example usage:
# sol = Solution()
# print(sol.squareFreeSubsets([3,4,4,5]))  # Output: 3
# print(sol.squareFreeSubsets([1]))        # Output: 1
17000