Paths of length K

Count the number of length-K walks in a directed graph given its adjacency matrix, where K can be as large as 10^9, and output the count modulo 10^9+7.

Medium6MatrixGraphDivide and conquerMathInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

The country of Cheon has NN villages, numbered 1 to NN. Between two villages there may or may not be a road, and every road has length 1. A road can be traveled in one direction only.

Minho wrote the connections between the villages into an N×NN \times N adjacency matrix. If the jj-th number on line ii is 1, there is a road from village ii to village jj; if it is 0, that road does not exist. A 1 on the diagonal means the village has a road back to itself.

A path of length KK is a sequence of village numbers v0,v1,,vKv_0, v_1, \dots, v_K such that for every 1tK1 \le t \le K there is a road from village vt1v_{t-1} to village vtv_t. A path may pass through the same village several times, and v0v_0 and vKv_K are unrestricted. Two paths count as different if their sequences differ in at least one position.

Given the adjacency matrix, count the different paths of length KK.

Input

The first line contains NN and KK, separated by one space. (1N1001 \le N \le 100, 1K1091 \le K \le 10^9)

Each of the next NN lines contains NN integers, each either 0 or 1, separated by spaces. Together they form the adjacency matrix.

Output

Print the number of paths of length KK modulo 109+710^9+7 on one line.

Hint

In the first example the six paths of length 2 are:

  • 1 → 2 → 3
  • 1 → 3 → 4
  • 2 → 3 → 4
  • 3 → 4 → 1
  • 4 → 1 → 2
  • 4 → 1 → 3