human
What is a Python code to calculate the probability that the two boxes have the same number of distinct balls given the following:
Solve in Python:
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i. 
All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully).
Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully).
We want to calculate the probability that the two boxes have the same number of distinct balls.
 
Example 1:
Input: balls = [1,1]
Output: 1.00000
Explanation: Only 2 ways to divide the balls equally:
- A ball of color 1 to box 1 and a ball of color 2 to box 2
- A ball of color 2 to box 1 and a ball of color 1 to box 2
In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1

Example 2:
Input: balls = [2,1,1]
Output: 0.66667
Explanation: We have the set of balls [1, 1, 2, 3]
This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12):
[1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1]
After that we add the first two balls to the first box and the second two balls to the second box.
We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box.
Probability is 8/12 = 0.66667

Example 3:
Input: balls = [1,2,1,2]
Output: 0.60000
Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box.
Probability = 108 / 180 = 0.6

Example 4:
Input: balls = [3,2,1]
Output:...
assistant
#Here is Python code to calculate the probability that the two boxes have the same number of distinct balls.

class Solution:
    def getProbability(self, balls: List[int]) -> float:
        balls_flattened = []
        for i, count in enumerate(balls):
            balls_flattened.extend([i] * count)
        
        def has_same_distinct_color(perm):
            n = len(perm)
            return len(set(perm[: n // 2])) == len(set(perm[n // 2 :]))
        
        total = [0]
        same_distinct_color = [0]
        def get_perm(cur, balls):
            if not balls:
                total[0] += 1
                same_distinct_color[0] += has_same_distinct_color(cur)
                return
            for i in range(len(balls)):
                if i > 0 and balls[i] == balls[i - 1]:
                    continue
                get_perm(cur + [balls[i]], balls[:i] + balls[i + 1 :])
        
        get_perm([], balls_flattened)
        return same_distinct_color[0] / total[0]
    

    def getProbability(self, balls: List[int]) -> float:
        
        factorial_cache = [1]
        for i in range(1, sum(balls) + 1):
            factorial_cache.append(factorial_cache[-1] * i)
                
        def get_perm_count(arr):
            total = factorial_cache[sum(arr)]
            for a in arr:
                total //= factorial_cache[a]
            return total
        
        total_perms = get_perm_count(balls)
        valid_perms = [0]
        
        def find_valid_split(n, start, distinct1, group1, distinct2, group2):
            if n == 0:
                if distinct1 == distinct2:
                    valid_perms[0] += get_perm_count(group1) * get_perm_count(group2)
                return
            
            for i in range(start, len(group1)):
                if group1[i] == 0:
                    continue
                
                new_num1 = group1[i] - 1
                new_distinct1 = distinct1 if new_num1 > 0 else distinct1 - 1
                new_num2 = group2[i] + 1
                new_distinct2 = distinct2 if new_num2 > 1 else distinct2 + 1
                if new_distinct1 <...