#P9759. [COCI 2022/2023 #3] Bomboni

[COCI 2022/2023 #3] Bomboni

Problem Description

Iva is a crazy candy fan! In front of her there is an n×nn \times n grid filled with candies and obstacles. Iva is currently at the top-left corner. By moving right or down, she needs to reach the bottom-right corner. The cell where Iva currently stands has no obstacle.

In each cell there is a number indicating either a candy or an obstacle. Iva will eat all candies she passes through (including the candies at the start and the end) and multiply the corresponding numbers. Iva knows that her favorite number is kk, so she wants this product to be divisible by kk. She wants to know how many such paths there are. Since the answer may be very large, she only wants the result modulo 998244353998244353.

Input Format

The first line contains two integers n,kn, k, denoting the side length of the grid and Iva's favorite number.

In the next nn lines, each line contains nn numbers describing the grid. If ai,j=1a_{i,j} = -1, then that cell is an obstacle; otherwise, that cell contains a number satisfying 1ai,j1061 \le a_{i,j} \le 10^6.

Output Format

Output one integer, the answer.

2 2
3 2
1 4
2
3 6
5 2 -1
7 3 6
-1 3 1
3

Hint

Sample Explanation #2.

There are three such paths:

  • 5-2-3-3-1
  • 5-2-3-6-1
  • 5-7-3-6-1

Constraints.

Subtask Points Special Properties
11 1313 n,k,ai,j20n, k, a_{i,j} \le 20
22 1717 n,k20n, k \le 20
33 3333 k20k \le 20
44 4747 No special properties

For 100%100\% of the testdata, 1n5001 \le n \le 500, 1k1061 \le k \le 10^6, and 1ai,j106-1 \le a_{i,j} \le 10^6.

The full score for this problem is 110110 points.

Translated by ChatGPT 5