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