#include <bits/stdc++.h>
using namespace std;
typedef struct node
{
	int a[51][51];
	struct node operator *(const struct node & tmp)
	{
		struct node ret;
		memset(ret.a,0,sizeof(ret.a));
		int i,j,k;
		for(k=1;k<=50;k++)
		{
			for(i=1;i<=50;i++)
			{
				for(j=1;j<=50;j++)
				{
					if(a[i][k]&&tmp.a[k][j])
						ret.a[i][j]=((long long)a[i][k]*tmp.a[k][j]+ret.a[i][j])%1000000007;
				}
			}
		}
		return ret;
	}
}NODE,*PNODE;
NODE g,ans;
int main()
{
	int i,n,j,flag=0,s=0;
	long long K;
	scanf("%d %lld",&n,&K);
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			scanf("%d",&g.a[i][j]);
	while(K)
	{
		if(K%2)
		{
			if(!flag)
			{
				ans=g;
				flag=1;
			}
			else
				ans=ans*g;
		}
		g=g*g;
		K/=2;
	}
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			if(ans.a[i][j])
				s=(s+ans.a[i][j])%1000000007;
	printf("%d",s);
	return 0;
}