Given an N by N matrix A and K, compute A + A^2 + ... + A^K with all entries reduced modulo M.
You are given an N×NN \times NN×N matrix AAA and integers KKK and MMM. Reducing every entry of AKA^KAK modulo MMM is not hard.
This task asks for something else. Define the matrix SSS as
S=A+A2+A3+⋯+AKS = A + A^2 + A^3 + \cdots + A^KS=A+A2+A3+⋯+AK
and print every entry of SSS modulo MMM.
The first line contains NNN, KKK, and MMM separated by spaces (1≤N≤501 \le N \le 501≤N≤50, 1≤K≤1091 \le K \le 10^91≤K≤109, 1≤M≤1041 \le M \le 10^41≤M≤104).
Each of the next NNN lines contains the matrix AAA, NNN integers per line separated by spaces (−104≤Aij≤104-10^4 \le A_{ij} \le 10^4−104≤Aij≤104).
Print NNN lines. Each line holds the NNN entries of the corresponding row of SSS reduced modulo MMM, separated by single spaces.
Every remainder must be at least 000 and less than MMM. Entries of AAA can be negative, so watch the sign of the remainder.