#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll m=1000000007;
vector<vector<ll> > multi(vector<vector<ll> >a,vector<vector<ll> >b)
{
		ll n=a.size();
	vector<vector<ll> >c(n,vector<ll>(n,0));
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<n;j++)
		{
			//c[i][j]=0;
			for(ll k=0;k<n;k++)
			{
				c[i][j]=((a[i][k]%m*b[k][j]%m)%m+c[i][j]%m)%m;
			}
		}
	}
	return c;
}
vector<vector<ll> > power(vector<vector<ll> >g,ll k)
{
	if(k==1)return g;
	vector<vector<ll> >res;
	res=power(g,k/2);
	res=multi(res,res);
	if(k%2!=0)res=multi(res,g);
	return res;
}
int main()
{
	ll n,k;
	cin>>n>>k;
	vector<vector<ll> >g(n,vector<ll>(n,0));
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<n;j++)
		{
			cin>>g[i][j];
		}
	}
	vector<vector<ll> >res;
	res=power(g,k);
	ll sum=0;
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<n;j++)
		{
			sum=(res[i][j]%m+sum%m)%m;
		}
	}
	cout<<sum<<endl;
	return 0;
}