Please solve the programming task below using a self-contained code snippet in a markdown code block.

There are n people and 40 types of hats labeled from 1 to 40.
Given a 2D integer array hats, where hats[i] is a list of all hats preferred by the ith person.
Return the number of ways that the n people wear different hats to each other.
Since the answer may be too large, return it modulo 109 + 7.
 
Please complete the following python code precisely:
```python
class Solution:
    def numberWays(self, hats: List[List[int]]) -> int:
```