Strange Dream

Time limit1sMemory limit128 MB

Summary
Count ways to pick plates from boxes in a forward then backward pass so the recorded product is divisible by k, modulo l.
Level

Hard8 of 10

Topics
Dynamic programming, Number theory, Combinatorics
Solved
No attempts yet

Problem

Dumitru had a very strange dream: he was locked inside a room. In that room there were nn boxes, and each box contained exactly mm plates. Every plate has a single integer greater than or equal to 11 written on it. There was also a note in the room with two integers kk and ll on it, describing the following task.

  • Step 1: Take one plate from the first box, write the number on it into your notebook, change the number on that plate to 11, and put the plate back into the box. Then, in the same way, take one plate from the second box, then one from the third box, …\dots, up to and including the nn-th (last) box. Each time, take one plate from the current box, write its number in the notebook, change that plate's number to 11, and put it back.
  • Step 2: After that, in the same way, take one plate from box n−1n-1, then one from box n−2n-2, …\dots, down to and including the second box. Each time, take one plate from the current box, write its number in the notebook, change that plate's number to 11, and put it back.

Let TT be the number of different ways of picking the plates as described above such that the product of all the numbers written in the notebook is divisible by kk. Because TT can be extremely large, compute the remainder of TT when divided by ll.

Input

The first line contains two integers nn and mm separated by a single space. The second line contains two integers kk and ll separated by a single space. Then follow nn lines, each containing mm integers separated by single spaces. The first of these lines holds the mm numbers written on the plates in the first box, the second line holds the numbers of the second box, and so on.

Output

Print a single integer: the remainder of TT when divided by ll.

Constraints

  • 3≤n≤2003 \le n \le 200
  • 3≤m≤10 0003 \le m \le 10\,000
  • 2≤k≤200 0002 \le k \le 200\,000
  • 2≤l≤30 0002 \le l \le 30\,000
  • Each number written on a plate is an integer between 11 and 1 000 0001\,000\,000 inclusive.

Explanation

In the first example there are exactly 1212 ways of picking the plates so that the product of the numbers written in the notebook is divisible by 1212. Two points are important.

  • If in some box you pick the same plate in both Step 1 and Step 2, then its number was already changed to 11 during Step 1, so the value written in the notebook during Step 2 is 11.
  • Two ways are counted as different whenever the combination of chosen plate indices differs, even if the multiset of picked plate values is the same.

Examples3

  1. Example 1

    Input
    3 3
    12 100
    5 2 1
    2 1 2
    3 7 4
    
    Expected output
    12
    
  2. Example 2

    Input
    3 3
    2 7
    1 1 1
    1 1 1
    1 1 1
    
    Expected output
    0
    
  3. Example 3

    Input
    3 3
    4 1000
    1 1 1
    2 2 2
    1 1 1
    
    Expected output
    54