#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define EB emplace_back
#define MP make_pair
#define all(o) (o).begin(), (o).end()
#define mset(m,v) memset(m,v,sizeof(m))
#define fr(i,n) for(lli i=0;i<n;++i)
#define rep(i,a,b) for(lli i=a;i<=b;++i)
#define per(i,a,b) for(lli i=a;i>=b;i--)
#define remin(a,b) (a=min((a),(b)))
#define remax(a,b) (a=max((a),(b))) 
#define sz(x) (lli)(x).size()
#define endl '\n'
typedef long long lli;        typedef long double ld;
typedef pair<lli,lli> ii;     typedef vector<lli> vi;
typedef vector<ii> vii;       typedef vector<vi> graph;
long long MOD=1000000007;     long double EPS=1e-9;
#define debarr(a,n) cout<<#a<<" : ";for(lli i=0;i<n;i++) cerr<<a[i]<<" "; cerr<<endl;
#define debmat(mat,row,col) cout<<#mat<<" :\n";for(lli i=0;i<row;i++) {for(lli j=0;j<col;j++) cerr<<mat[i][j]<<" ";cerr<<endl;}
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class S, class T>ostream& operator <<(ostream& os, const pair<S, T>& p) {return os << "(" << p.first << ", " << p.second << ")";}
template <class T>ostream& operator <<(ostream& os, const vector<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const unordered_set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const unordered_map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const multiset<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T> void dbs(string str, T t) {cerr << str << " : " << t << "\n";}
template <class T, class... S> void dbs(string str, T t, S... s) {lli idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ","; dbs(str.substr(idx + 1), s...);}
template <class T> void prc(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) {if (i != a) cerr << ", "; cerr << *i;} cerr << "]\n";}
lli binpow(lli b,lli p,lli mod){lli ans=1;b%=mod;for(;p;p>>=1){if(p&1)ans=ans*b%mod;b=b*b%mod;}return ans;}

void pre(){

}
typedef vector<vi> matrix;

const lli matrixmod = MOD;

matrix matrixUnit(lli n) {
    matrix res(n, vi(n));
    for (lli i = 0; i < n; i++)
        res[i][i] = 1;
    return res;
}

matrix matrixAdd(const matrix &a, const matrix &b) {
    lli n = a.size();
    lli m = a[0].size();
    matrix res(n, vi(m));
    for (lli i = 0; i < n; i++)
        for (lli j = 0; j < m; j++)
            res[i][j] = (a[i][j] + b[i][j]) % matrixmod;
    return res;
}

matrix matrixMul(const matrix &a, const matrix &b) {
    lli n = a.size();
    lli m = a[0].size();
    lli k = b[0].size();
    matrix res(n, vi(k));
    for (lli i = 0; i < n; i++)
        for (lli j = 0; j < k; j++)
            for (lli p = 0; p < m; p++)
                res[i][j] = (res[i][j] + (long long) a[i][p] * b[p][j]) % matrixmod;
    return res;
}

matrix matrixPow(const matrix &a, lli p) {
    if (p == 0)
        return matrixUnit(a.size());
    if (p & 1)
        return matrixMul(a, matrixPow(a, p - 1));
    return matrixPow(matrixMul(a, a), p / 2);
}

matrix matrixPowSum(const matrix &a, lli p) {
    lli n = a.size();
    if (p == 0)
        return matrix(n, vi(n));
    if (p % 2 == 0)
        return matrixMul(matrixPowSum(a, p / 2), matrixAdd(matrixUnit(n), matrixPow(a, p / 2)));
    return matrixAdd(a, matrixMul(matrixPowSum(a, p - 1), a));
}
// Create new as matrix a(#of rows,vi(#of cols))
void solve(){
    lli n,k;
    cin>>n>>k;
    matrix aa(n,vi(n,0));
    fr(i,n){
      fr(j,n){
        cin>>aa[i][j];
      }
    }
    aa=matrixPow(aa,k);
    lli ans=0;
    fr(i,n){
      fr(j,n){
        ans=(ans+aa[i][j])%MOD;
      }
    }
    cout<<ans<<endl;
}

signed main(){
  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  //freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);
  pre();lli t=1;//cin>>t;
  for(lli i=1;i<=t;i++){
    //cout<<"Case #"<<i<<": ";
    solve();
  }
}

Measured memory_bytes: 1664