## DMOPC '21 Contest 8 P2 - Kanna Market

View as PDF

Points: 10 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem types

Kanna is going shopping!

Kanna's shopping market can be modeled as a grid of shops. It is known that every square of cells contains exactly enough shops to get everything they need, and hence, parents always visit cells in a square.

Of course, each cell contains a stand giving out cookies to children! Having been there many times, Kanna realizes that no matter which square they shop in, she always gets exactly the same amount of cookies from the four cells they visit.

Kanna has obtained some data indicating the amount of cookies available at each cell, represented as a grid . However, it is incomplete. She knows however, that each cell gives out at least one cookie and none give out more than .

She wonders: assuming every square gives the same amount of cookies, how many arrangements of cookie counts to the remaining cells are possible? Note that Kanna's data may be incorrect, so it is possible that no arrangement is consistent with her data.

#### Input Specification

The first line of input contains space-separated integers and .

The second line contains integers, the first row of the grid.

The third line contains integers, the second row of the grid.

Each cell of the grid is either if Kanna has no data on the stand or a positive integer at most otherwise, indicating the amount of cookies given out there.

#### Output Specification

Output the number of ways to fill the grid such that every square has the same sum. Since this number may be very large, output it modulo .

#### Sample Input 1

3 2
2 1 0
0 1 0

#### Sample Output 1

3

#### Explanation for Sample 1

The three grids are:

2 1 1
1 1 2
2 1 2
1 1 1
2 1 2
2 1 2

#### Sample Input 2

3 20
1 1 1
1 1 1

#### Sample Output 2

1