<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>

"""
[3,4,4,5]
[1]
[2,3]
[1,1,2]
[1,2,6,15,7,19,6,29,28,24,21,25,25,18,9,6,20,21,8,24,14,19,24,28,30,27,13,21,1,23,13,29,24,29,18,7]
"""
class Solution:
    conv = {2: 0, 3: 1, 5: 2, 7: 3, 11: 4, 13: 5, 17: 6, 19: 7, 23: 8, 29: 9}
    bm_all = [0]

    def get_bm(v):
        if v > 1:
            res = 0
            if v % 2 == 0:
                res |= (1 << Solution.conv[2])
                while v % 2 == 0:
                    v //= 2
            for i in range(3, v // 2 + 1, 2):
                if v % i == 0:
                    res |= 1 << Solution.conv[i]
                    while v % i == 0:
                        v //= i
            if v > 1:
                res |= 1 << Solution.conv[v]
        else:
            res = -1
        return res

    def squareFreeSubsets(self, nums: List[int]) -> int:
        # 1 <= nums[i] <= 30, 1 <= nums.length <= 1000
        # prime factor non-repeating
        # 1) 1 to 30, [9, 18...], [4, 8, ...], [25] are out
        # 2) each number 1 to 30 has a bm representing prime factors (2 to 29)
        if len(Solution.bm_all) == 1:
            for v in range(1, 31):
                Solution.bm_all.append(Solution.get_bm(v))

        # pure dp will MLE shit
        # use Counter
        ct = Counter()
        for v in nums:
            if v % 9 == 0 or v % 4 == 0 or v == 25:
                continue
            ct[Solution.bm_all[v]] += 1
        items = list(ct.items())

        @cache
        def dp(i, bm):
            if i < n:
                res = dp(i + 1, bm)
                if items[i][0] >= 0:
                    if bm & items[i][0] == 0:
                        res = (res + items[i][1] * dp(i + 1, bm | items[i][0])) % MOD
                else: # bm == -1 representing "1", and the factors for 1 is 2**ct[1] - 1
                    res = (res + (pow(2, items[i][1], MOD) - 1) * dp(i + 1, bm)) % MOD
                return res
            return 1
        
        n = len(items)
        MOD = 1_000_000_007
        return (dp(0, 0) - 1) % MOD
        
22200