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

#define sz(a) int(a.size())
#define reset(a,b) memset(a,b,sizeof(a))
#define pb					push_back
#define eb					emplace_back
#define mp					make_pair
#define endl 				'\n'
#define gc					getchar_unlocked
#define pc					putchar_unlocked
#define FASTIO std::ios::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL);
#define N 1000000
#define MAX_DIGITS 7 // 7 digits in 1 000 000
static const long long buf_len = (1 << 14);
static const long long buf_max = (1 << 04);
static char buf_out[buf_len];
static char buf_num[buf_max];
static long long buf_pos = 0;
#ifdef _WIN32
#define GETCHAR() _getchar_nolock()
#define PUTCHAR(c) _putchar_nolock(c)
#elif __unix__
#define GETCHAR() getchar_unlocked()
#define PUTCHAR(c) putchar_unlocked(c)
#else
#define GETCHAR() getchar()
#define PUTCHAR(c) putchar(c)
#endif
#define MOD 1000000007  
#define lld long long 
static const lld nax=1e6+5;
short ch = 0;

 inline string ipst()
{
    string i="";
    long long temp=getchar_unlocked();
    while(temp<'a'||temp>'z')
        temp=getchar_unlocked();
    while(temp>='a'&&temp<='z')
    {
        i+=(char)temp;
        temp=getchar_unlocked();
    }
    return i;
}

 inline int getLine(string &str) {
        str = "";
        if (ch == -1) return -1;
        while (ch < 33 || ch == 127) {ch = getchar_unlocked(); if (ch == -1) return -1;}
        while (ch > 31 && ch < 127) {
            str.push_back(ch);
            ch = getchar_unlocked();
        }
        return str.size();
 }

inline void opst(string &str) {
        long long len = str.length();
        for (int i=0; i<len; i++) putchar_unlocked(str[i]);
}

 inline lld cinll(){
	char r;
	bool start=false,neg=false;
	lld ret=0;
	while(true){
		r=getchar();
		if((r-'0'<0 || r-'0'>9) && r!='-' && !start){
			continue;
		}
		if((r-'0'<0 || r-'0'>9) && r!='-' && start){
			break;
		}
		if(start)ret*=10;
		start=true;
		if(r=='-')neg=true;
		else ret+=r-'0';
	}
	if(!neg)
		return ret;
	else
		return -ret;
}
 
 inline void coutll(int n)
{
    if(n == 0)
    {
        PUTCHAR('0');
        return;
    }
 
    if(n < 0)
    {
        PUTCHAR('-');
        n = -n;
    }
 
    char buffer[MAX_DIGITS];
    int i = MAX_DIGITS - 1;
 
    while(n)
    {
        buffer[i--] = n % 10 + '0';
        n /= 10;
    }
 
    while(i != MAX_DIGITS - 1) 
    {
        PUTCHAR(buffer[++i]);
    }
}

const lld INF=1e9+5;
const lld mod=1e9+7;

void add(int& a,int b)
{
	a+=b;
	if(a>=mod)
	a-=mod;
}
void sub(int& a,int b)
{
	a-=b;
	if(a<0)
	a+=mod;
}

int mul(int a,int b)
{
	return (lld)a*b%mod;
}

int prefixsum[3005][3005];

/*void prefix_sum(int len)
{
	for(int i=1;i<len;i++)
	{
		prefixsum[len-1][i]=(prefixsum[len-1][i-1]+dp[len-1][i])%mod;
	}
}*/

int n;

struct magic{
	vector<vector<int>>t;
	magic()
	{
		t.resize(n,vector<int>(n));
	}
	magic operator*(const magic& b)const
	{
		magic c=magic();
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				for(int k=0;k<n;k++)
				{
					add(c.t[i][k],mul(t[i][j],b.t[j][k]));
				}
			}
		}
		return c;
	}
};
int main() {
    FASTIO
    lld k;
    cin>>n;
    cin>>k;
    vector<vector<int>> can(n,vector<int>(n));
    for(int i=0;i<n;i++)
    {
    	for(int j=0;j<n;j++)
    	{
    		cin>>can[i][j];
    	}
    }
    magic res=magic();
    for(int i=0;i<n;i++)
    {
    	res.t[i][i]=1;
    }
    magic m=magic();
    m.t=can;
    while(k)
    {
    	if(k%2!=0)
    	{
    		res=res*m;
    	}
    	m=m*m;
    	k/=2;
    }
    int kq=0;
    for(int i=0;i<n;i++)
    {
    	for(int j=0;j<n;j++)
    	{
    		add(kq,res.t[i][j]);
    	}
    }
    coutll(kq);
    assert(1);
    system("pause");
    return 0;
}

Measured memory_bytes: 3668