<p>An array is <strong>squareful</strong> if the sum of every pair of adjacent elements is a <strong>perfect square</strong>.</p>

<p>Given an integer array nums, return <em>the number of permutations of </em><code>nums</code><em> that are <strong>squareful</strong></em>.</p>

<p>Two permutations <code>perm1</code> and <code>perm2</code> are different if there is some index <code>i</code> such that <code>perm1[i] != perm2[i]</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,17,8]
<strong>Output:</strong> 2
<strong>Explanation:</strong> [1,8,17] and [17,8,1] are the valid permutations.
</pre>

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

<pre>
<strong>Input:</strong> nums = [2,2,2]
<strong>Output:</strong> 1
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 12</code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def permutationBuilder(self, num_counts: {}, squares: {}, partial_permutation: List[int], answer):

        bottom = True

        # Create a copy so each depth of recursion does not effect the others
        partial_permutation = list(partial_permutation)

        # For each category of values, check if there are any copies left, if there is, mark this depth as not the bottom and check if
        # the selected value plus the last item in the permutation sum to a perfect square. If they so, add the value to the permutation 
        # and recursively call this function again. After returning from that call, remove the last item from the list and add it back 
        # to the category. Then loop to the next category and repeat. If all categories are empty, we've reached the bottom and record 
        # the permutation in the answer list.
        for val, count in num_counts.items():
            if count > 0:
                bottom = False
                if val + partial_permutation[-1] in squares:
                    num_counts[val] -= 1
                    partial_permutation.append(val)
                    self.permutationBuilder(num_counts, squares, partial_permutation, answer)
                    partial_permutation = partial_permutation[:-1]
                    num_counts[val] += 1
        
        if bottom:
            answer[0] += 1
        return

    def numSquarefulPerms(self, nums: List[int]) -> int:

        if len(nums) < 2:
            return 0

        count = 0
        squares = {}

        while count * count <= 1000000000:
            squares[count * count] = 1
            count += 1

    

        nums_counted = {}

        # Count up the occurrences of each value and record them in a dictionary
        for i in nums:
            if i in nums_counted:
                nums_counted[i] += 1
            else:
                nums_counted[i] = 1

        answer = [0]
        partial_permutation = []

        # For each category, pull one copy from the dictionary and add it to the beginning of the list, then call
        # the recursive function. Return the item to the category after returning from the recursive call.
        for val, count in nums_counted.items():
            nums_counted[val] -= 1
            partial_permutation = [val]
            self.permutationBuilder(nums_counted, squares, partial_permutation, answer)
            nums_counted[val] += 1

        return answer[0]
        
19700