You're organizing a costume party where n people need to wear different hats! There are 40 types of hats available (labeled 1 to 40), and each person has their own list of preferred hats they're willing to wear.
Your challenge: Count how many ways all n people can wear different hats from each other, where each person wears exactly one hat from their preference list, and no two people wear the same hat.
Input: A 2D array hats where hats[i] contains all hat types that person i is willing to wear.
Output: The number of valid arrangements modulo 109 + 7.
Example: If person 0 likes hats [1,2] and person 1 likes hats [2,3], there are 2 ways: person 0 wears hat 1 & person 1 wears hat 2, OR person 0 wears hat 1 & person 1 wears hat 3.
Input & Output
Visualization
Time & Space Complexity
40 hats, 2^n possible masks, and up to n people can wear each hat
DP table with 40 hats and 2^n possible people assignments
Constraints
-
n == hats.length -
1 โค n โค 10 -
1 โค hats[i].length โค 40 -
1 โค hats[i][j] โค 40 - All values in hats[i] are unique
-
The total number of hats across all people is at most
40