Vi and Ekko are working together to try to make a map of
Zaun. Within the city of Zaun, an underground network of
tunnels connects
locations, labeled
through . This network
forms a connected system where exactly one unique path exists
between any two locations, and each tunnel segment connects two
locations. Ekko, starting from the Firelights’ hideout
(location ), and Vi,
starting from the The Last Drop (location ), have each independently measured
the exact number of tunnel segments required to reach every
other location in the network. Given these distance
measurements from both Ekko’s and Vi’s starting points,
determine how many unique possible layouts of the tunnel
network could exist while still satisfying both sets of
recorded distances. Since the answer may be very large, find
the answer modulo . It is possible that Vi or
Ekko made a mistake so that no possible layout could exist
according to their measurements.
Input
The first line of input is an integer . The
second line contains
space-separated integers, , where
is the distance from
location to the
Firelights’ hideout. The third line contains space-separated integers,
, where is the distance from location
to The Last Drop. The
Firelights’ hideout is at location , and The Last Drop is at location
.
Output
Output one line containing a single integer, the number of
unique tunnel network layouts, modulo .
Sample Input 1 |
Sample Output 1 |
5
0 1 1 1 2
1 0 2 2 3
|
2
|
Sample Input 2 |
Sample Output 2 |
3
0 1 1
1 0 1
|
0
|