## March Break Contest '22 Problem 1 - Bob's Dice Game

View as PDF

Points: 7 (partial)
Time limit: 2.0s
Memory limit: 64M

Author:
Problem type

Bob is playing a new game that he has discovered over March Break. To play the game, he has bought a -sided die with faces numbered to which he must roll times in a row. Bob succeeds on the th roll if the number he rolls is at least .

In order to increase the number of rolls that he succeeds, Bob will use his portent ability inside the game which will allow him to replace up to two of the rolls with two values that he had rolled beforehand, and . Note that Bob may only choose to replace a roll after rolling the die for that roll.

Bob would like to know the number of possible dice roll configurations, out of a total of , such that he can succeed on all rolls with optimal use of his ability. Since this number may be very large, Bob would like to know its value modulo .

#### Input Specifications

The first line will contain two space-separated integers, and .

The next line will contain two space-separated integers, and .

The next line will contain space-separated integers, .

No further constraints.

#### Output Specifications

Output one integer, the number of possible dice configurations such that Bob can succeed on all rolls, modulo .

#### Sample Input 1

3 3
2 3
2 3 3

#### Sample Output 1

15

#### Sample Input 2

5 20
8 20
9 15 12 19 5

#### Sample Output 2

368640