National Olympiad in Informatics, China, 2010
The 2010 World Expo will be hosted in Shanghai, China, attracting millions of tourists from within and outside of China. Little Z has also arrived at Expo 2010 during her summer vacation. She has known about the infamous crowdedness of the world's fair. She is already very prepared to wait many hours in line at certain pavilions, but to make her trip at the Expo even smoother, little Z decided to plan a detailed route.
Little Z has gotten her hands on a map of the Expo, and she noticed that
the site of the whole event is very narrow and long. Within it, each
pavilion occupies a square region which are all roughly the same size.
Thus, we can view the entire Expo as an
Since the attention received by different pavilions differ, the length
of time spent waiting in their lines will also vary. Through statistical
data, Little Z has labeled each pavilion
At the same time, little Z really values efficiency; she wishes to avoid unnecessary walking when visiting all the pavilions. Thus she wishes for her route to satisfy the following conditions:
- After visiting the pavilion at location
, the next pavilion she visits must be a neighboring pavilion that has not already been visited, where . - The route's starting position must be on the border of the entire
grid, where either
, or , or , or .
Little Z wishes to know how many possible routes can satisfy these
requirements. Since the final answer can be really large, little Z would
only like to know the number of possible routes modulo
Input Specification
- The first line contains two positive integers
and . - For lines
to , each line contains space separated integers, where the -th number on line represents . - Line
contains a space separated integers: the -th integer is the value of .
Output Specification
The output should contain a single integer, representing the number of
valid routes that satisfy the requirements, modulo
Sample Input
2 2
1 0
0 1
1 0 1 0
Sample Output
4
Explanation
The four valid routes are:
Constraints
For
For
For
For
Problem translated to English by , fixed by .
Comments