#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
using ll=long long;

vector<vector<long long>> multi(vector<vector<long long>> m1,vector<vector<long long>>m2) //正方行列に対する積
{
	int n=m1.size();
	vector<vector<long long>> ans(n,vector<long long> (n,0));
	for (int i=0;i<n;i++)
		for (int j=0;j<n;j++)
			for (int k=0;k<n;k++)
			{
				ans[i][j]=(ans[i][j]+m1[i][k]*m2[k][j]%mod)%mod;
			}
	
return ans;
}
vector<vector<long long>> power(vector<vector<long long>> m1,long long r)
{
	if (r==1) return m1;
	else
	{
		vector<vector<long long>> ans;
		ans=power(m1,r/2);
		if (r%2==0) return multi(ans,ans);
		else return multi(multi(ans,ans),m1);
	}	
}

int main()
{
	ll N,K; cin >> N>>K;
	vector<vector<ll>> a(N,vector<ll> (N));
	for (int i=0;i<N;i++)
		for (int j=0;j<N;j++)
			cin >> a[i][j];
	a=power(a,K);
	ll ans=0;
	for (int i=0;i<N;i++)
		for (int j=0;j<N;j++) ans=(ans+a[i][j])%mod;
	cout << ans << endl;
return 0;
}