Problem:
Score : 100 points

Problem Statement

There is a simple directed graph G with N vertices, numbered 1, 2, \ldots, N.

For each i and j (1 \leq i, j \leq N), you are given an integer a_{i, j} that represents whether there is a directed edge from Vertex i to j.
If a_{i, j} = 1, there is a directed edge from Vertex i to j; if a_{i, j} = 0, there is not.

Find the number of different directed paths of length K in G, modulo 10^9 + 7.
We will also count a path that traverses the same edge multiple times.

Constraints

All values in input are integers.

1 \leq N \leq 50

1 \leq K \leq 10^{18}

a_{i, j} is 0 or 1.

a_{i, i} = 0

Input

Input is given from Standard Input in the following format:

N K
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}

Output

Print the number of different directed paths of length K in G, modulo 10^9 + 7.

Sample Input 1

4 2
0 1 0 0
0 0 1 1
0 0 0 1
1 0 0 0

Sample Output 1

6

G is drawn in the figure below:

There are six directed paths of length 2:

1 → 2 → 3

1 → 2 → 4

2 → 3 → 4

2 → 4 → 1

3 → 4 → 1

4 → 1 → 2

Sample Input 2

3 3
0 1 0
1 0 1
0 0 0

Sample Output 2

3

G is drawn in the figure below:

There are three directed paths of length 3:

1 → 2 → 1 → 2

2 → 1 → 2 → 1

2 → 1 → 2 → 3

Sample Input 3

6 2
0 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 0
0 0 0 0 1 0
0 0 0 0 0 1
0 0 0 0 0 0

Sample Output 3

1

G is drawn in the figure below:

There is one directed path of length 2:

4 → 5 → 6

Sample Input 4

1 1
0

Sample Output 4

0

Sample Input 5

10 1000000000000000000
0 0 1 1 0 0 0 1 1 0
0 0 0 0 0 1 1 1 0 0
0 1 0 0 0 1 0 1 0 1
1 1 1 0 1 1 0 1 1 0
0 1 1 1 0 1 0 1 1 1
0 0 0 1 0 0 1 0 1 0
0 0 0 1 1 0 0 1 0 1
1 0 0 0 1 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
1 0 1 1 1 0 1 1 1 0

Sample Output 5

957538352

Be sure to print the count modulo 10^9 + 7.

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.Scanner;

public class Main {

	static Scanner sc = new Scanner(System.in);
	static final long MOD = 1_000_000_007;

	public static void main(String[] args) {
		int N = sc.nextInt();
		long K = sc.nextLong();
		Matrix mat = new Matrix(N, N);
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				mat.v[i][j] = sc.nextInt();
			}
		}
		mat = mat.pow(K);
		long ans = 0;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				ans += mat.v[i][j];
			}
		}
		System.out.println(ans % MOD);
	}

	static class Matrix {

		long[][] v;

		Matrix(int dh, int dw) {
			this.v = new long[dh][dw];
		}

		Matrix mul(Matrix o) {
			Matrix ret = new Matrix(this.dim1(), o.dim2());
			for (int i = 0; i < this.dim1(); ++i) {
				for (int j = 0; j < o.dim2(); ++j) {
					for (int k = 0; k < this.dim2(); ++k) {
						ret.v[i][j] += this.v[i][k] * o.v[k][j];
						ret.v[i][j] %= MOD;
					}
				}
			}
			return ret;
		}

		Matrix pow(long p) {
			Matrix ret = new Matrix(this.dim1(), this.dim1());
			for (int i = 0; i < this.dim1(); ++i) {
				ret.v[i][i] = 1;
			}
			Matrix base = this.clone();
			while (p > 0) {
				if (p % 2 != 0) {
					ret = ret.mul(base);
				}
				p /= 2;
				base = base.mul(base);
			}
			return ret;
		}

		int dim1() {
			return this.v.length;
		}

		int dim2() {
			return this.v[0].length;
		}

		public Matrix clone() {
			Matrix ret = new Matrix(this.dim1(), this.dim2());
			for (int i = 0; i < this.dim1(); ++i) {
				for (int j = 0; j < this.dim2(); ++j) {
					ret.v[i][j] = this.v[i][j];
				}
			}
			return ret;
		}
	}
}