#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N=50,zqm=1e9+7;
int n;
struct mat {
	int d[N][N];
	int* operator[](const int k) {return d[k];}
	const int* operator[](const int k) const {return d[k];}
	mat(bool isunit=0) {
		memset(d,0,sizeof(d));
		if(isunit) for(int i=0;i<n;++i) d[i][i]=1;
	}
	mat operator*(const mat &rhs) const {
		mat ret;
		for(int k=0;k<n;++k) {
			for(int i=0;i<n;++i) if(d[i][k]) {
				for(int j=0;j<n;++j) (ret[i][j]+=(d[i][k]*1ll*rhs[k][j])%zqm)%=zqm;
			}
		}
		return ret;
	}
}a,b;

int main() {
	//freopen("1.in","r",stdin);
	//freopen("1.ans","w",stdout);
	
	ll k; scanf("%d%lld",&n,&k);
	for(int i=0;i<n;++i) {
		for(int j=0;j<n;++j) scanf("%d",&a[i][j]);
	}
	b=mat(1);
	for(;k;k/=2,a=a*a) if(k&1) b=b*a;
	int ans=0;
	for(int i=0;i<n;++i) {
		for(int j=0;j<n;++j) (ans+=b[i][j])%=zqm;
	}
	printf("%d\n",ans);
	
	return 0;
}
