• + 0 comments
    def solve(c):
        # By sorting the cards, we ensure that we handle the smallest constraints first, which simplifies checking validity and calculating permutations
        c.sort()
    
        result = 1
    
        for i in range(len(c)):
    
            # For each card at index i, the value must be at most i. If any card's value exceeds its index, it can't be placed in a valid position, resulting in 0 permutations
            if c[i] > i:
                return 0
    
            # Permutations Calculation: For each valid card, the number of available positions is determined by (i + 1 - c[i]), where i is the index (0-based). The product of these values for all cards gives the total number of valid permutations
            result = (result * (i + 1 - c[i])) % 1000000007
    
        return result