Compute A + A^2 + ... + A^B for an N by N matrix A, with B up to 1e11, printing every entry modulo 1000.
You are given an N×NN \times NN×N matrix AAA. Write a program that computes the matrix obtained by adding every power of AAA from the first up to the BBBth:
S=A1+A2+⋯+ABS = A^1 + A^2 + \cdots + A^BS=A1+A2+⋯+AB
The entries can grow very large, so print each entry of SSS modulo 1,000.
The first line contains the matrix size NNN and the exponent bound BBB, separated by a space. (2≤N≤52 \le N \le 52≤N≤5, 1≤B≤100,000,000,0001 \le B \le 100{,}000{,}000{,}0001≤B≤100,000,000,000)
Each of the next NNN lines contains NNN entries of AAA, separated by spaces. Every entry is 0 or a natural number at most 1,000.
Print the matrix SSS on NNN lines. Line iii contains the NNN entries of row iii of SSS, each taken modulo 1,000 and separated by a space.