#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

typedef vector<ll> vec;
typedef vector<vec> mat;
const int mod=1000000007;
mat mul(mat &A,mat &B){
    mat C(A.size(),vec(B[0].size()));
    rep(i,A.size())rep(k,B.size())rep(j,B[0].size()){
        C[i][j]=(C[i][j]+A[i][k]*B[k][j])%mod;
    }
    return C;
}
mat pow(mat A,ll n){
    mat B(A.size(),vec(A.size()));
    rep(i,A.size()) B[i][i]=1;
    while(n>0){
        if(n&1)B=mul(B,A);
        A=mul(A,A);n>>=1;
    }
    return B;
}

int n;
ll k;
ll ai;
int main(){
    cin>>n>>k;
    mat a=vector<vector<ll> >(n,vector<ll>(n,0));
    rep(i,n)rep(j,n){
        cin>>ai;
        a[i][j]=ai;
    }
    a=pow(a,k);
    ll ans=0;
    rep(i,n)rep(j,n){
        ans+=a[i][j];
        ans%=mod;
    }
    cout<<ans<<endl;
    return 0;
}