How many possibilities?
Time limit2sMemory limit1024 MB
Count the key press sequences on a staggered N by N keyboard where consecutive pressed keys are at the given distances, modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Math, Geometry
- Solved
- No attempts yet
Problem
UFOs are steered with giant keyboards. Each keyboard has rows with keys in each row. Each row starts half a key further to the right than the row before it.

Two keys are neighbors if they touch along an edge. The distance between keys and is the minimum number of steps needed to get from to , where each step moves to a neighbor of the current key. In the figure, the distance between the keys
and
is 4.
A UFO war is underway. The enemy UFO has just started a complicated maneuver, and the pilot pressed keys to perform it. To plan a counter-maneuver, it would help to know which keys were pressed. We only know the distance between the first and second pressed keys, the distance between the second and third, and so on. How many different key press sequences match this information?
Since the true answer may be huge, print it modulo .
Input
The first line of the file contains two integers and (, ). The second line contains space-separated integers (), where is the distance between the -th and -th pressed keys.
Output
Print one integer: the number of possible key press sequences modulo .