E - Stolen Table

Languages: C, C++, Java, Python, Kotlin
Time & Memory limits: (details)

Input

Output

Sample test(s)

Input
3 3 2 2 3 2 3 3
Output
89
Input
1 1 1 2
Output
0
Input
5 5 2 2 3 3 3 2 2 2 3 3
Output
49049891
Input
12 13 2 2 2 3 3 4 4 4 4 5 5 5 2 3 3 3 3 4 5 5 5 5 5 5 5
Output
808346164
Input
2 3 2 3 3 1 5
Output
0