Sum of matrix powers

Compute A + A^2 + ... + A^B for an N by N matrix A, with B up to 1e11, printing every entry modulo 1000.

Medium7Divide and conquerMatrixRecursionNo attempts yetTime limit2sMemory limit512 MB

Problem

You are given an N×NN \times N matrix AA. Write a program that computes the matrix obtained by adding every power of AA from the first up to the BBth:

S=A1+A2++ABS = A^1 + A^2 + \cdots + A^B

The entries can grow very large, so print each entry of SS modulo 1,000.

Input

The first line contains the matrix size NN and the exponent bound BB, separated by a space. (2N52 \le N \le 5, 1B100,000,000,0001 \le B \le 100{,}000{,}000{,}000)

Each of the next NN lines contains NN entries of AA, separated by spaces. Every entry is 0 or a natural number at most 1,000.

Output

Print the matrix SS on NN lines. Line ii contains the NN entries of row ii of SS, each taken modulo 1,000 and separated by a space.