Construct Product Matrix
Given a 0-indexed 2D integer matrix grid of size n * m, define a 0-indexed 2D matrix p of size n * m as the product matrix of grid if the following condition is met:
- Each element
p[i][j]is calculated as the product of all elements ingridexcept for the elementgrid[i][j]; this product is then taken modulo12345.
Return the product matrix of grid.
Example 1
1 2 3 4
Input
grid = [[1,2],[3,4]]Output
[[24,12],[8,6]]Each cell contains the product of all other elements in
grid, producing [[24,12],[8,6]].Example 2
12345
2
1Input
grid = [[12345],[2],[1]]Output
[[2],[0],[0]]The products excluding each element are
2, 12345, and 24690, which modulo 12345 become 2, 0, and 0.Constraints
- 1 <= n == grid.length <= 10^5
- 1 <= m == grid[i].length <= 10^5
- 2 <= n * m <= 10^5
- 1 <= grid[i][j] <= 10^9