#include <bits/stdc++.h>

using namespace std;

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;

constexpr int MAXN = 50, MOD = 1000000007;

int dp[3][MAXN][MAXN];
int N;

void convolve(int c, int a, int b) {
	for(int i = 0; i < N; i++) {
		for(int j = 0; j < N; j++) {
			dp[c][i][j] = 0;
			for(int k = 0; k < N; k++)
				dp[c][i][j] = (dp[c][i][j] + (ll)dp[a][i][k] * dp[b][k][j]) % MOD;
		}
	}
}

void copy(int a, int b) {
	for(int i = 0; i < N; i++)
		for(int j = 0; j < N; j++)
			dp[a][i][j] = dp[b][i][j];
}

int main() {
	ll K;
	cin >> N >> K;
	for(int i = 0; i < N; i++)
		for(int j = 0; j < N; j++)
			cin >> dp[0][i][j];
	int base = 0, res = 1, other = 2;
	copy(res, base);
	K--;
	while(K) {
		if(K & 1) {
			convolve(other, base, res);
			swap(other, res);
		}
		convolve(other, base, base);
		swap(other, base);
		K >>= 1;
	}
	int ans = 0;
	for(int i = 0; i < N; i++)
		for(int j = 0; j < N; j++)
			ans = (ans + dp[res][i][j]) % MOD;
	cout << ans << endl;
}
