// Slower C++ solution:
#include <bits/stdc++.h>

#define ll long long

#define V vector<long long>

#define VV vector<vector<long long>>

#define rep(i,n) for(ll (i)=0;(i)<(n);++(i))

using namespace std;



struct suffix_array{



    int n,logn;

    vector<int> rank,sa;



    suffix_array(string s){

        n=s.size();

        logn=floor(log2(n));

        rank.resize(n);

        sa.resize(n);

        rep(i,n){

            rank[i]=s[i];

            sa[i]=i;

        }

        for(int k=0;k<logn;++k){

            auto comp=[&](int a,int b){

                if(rank[a]!=rank[b])return rank[a]<rank[b];

                a=(a+(1<<k)<n)?rank[a+(1<<k)]:-1;

                b=(b+(1<<k)<n)?rank[b+(1<<k)]:-1;

                return a<b;

            };

            sort(sa.begin(),sa.end(),comp);

            vector<int> tmp=rank;

            tmp[sa[0]]=0;

            for(int i=1;i<n;++i)tmp[sa[i]]=tmp[sa[i-1]]+comp(sa[i-1],sa[i]);

            rank=tmp;

        }

    }



    int operator[](int k)const{

        return sa[k];

    }



    int size()const{

        return n;

    }



};



const ll mod = 1e9+7;



struct Matrix{

  static const int maxn = 105;

  ll A[maxn][maxn];

  int n;



  Matrix(int n_): n(n_) {

    for(int i = 0; i < n; ++i)

      for(int j = 0; j < n; ++j)

        A[i][j] = 0;

  }



  Matrix operator *(const Matrix& o) const {

    Matrix ret(n);

    for(int i = 0; i < n; ++i)

      for(int j = 0; j < n; ++j)

        for(int k = 0; k < n; ++k){

          ll c = A[i][k] * o.A[k][j] % mod;

          ret.A[i][j] = (ret.A[i][j] + c) % mod;

        }

    return ret;

  }



  static Matrix identity(int n){

    Matrix ret(n);

    for(int i = 0; i < n; ++i)

      ret.A[i][i] = 1;

    return ret;

  }



  Matrix operator ^(long long k) const {

    Matrix ret = identity(n);

    Matrix a = *this;

    while(k){

      if(k & 1)ret = ret * a;

      a = a * a;

      k /= 2;

    }

    return ret;

  }



};



int main(){

    ll n,k;

    cin>>n>>k;

    Matrix M(n);

    rep(i,n){

        rep(j,n){

            cin>>M.A[j][i];

        }

    }

    M=M^k;

    ll ans=0;

    rep(i,n){

        rep(j,n){

            ans+=M.A[j][i];

            ans%=mod;

        }

    }

    cout<<ans<<endl;

}


// Faster C++ solution for the same problem (measured speedup: 2.4175765819444277x):
#include <bits/stdc++.h>



#define forn(i, n) for (int i = 0; i < int(n); ++i)

#define for1(i, n) for (int i = 1; i <= int(n); ++i)

#define F first

#define S second

#define all(x) (x).begin(),(x).end()

#define sz(x) int(x.size())

#define pb push_back



using namespace std;

using ll=long long;

using pii= pair<int, int>;

mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());

template<typename... T> void rd(T&... args) {((cin>>args), ...);}

template<typename... T> void wr(T... args) {((cout<<args<<" "), ...);cout<<endl;}



int n;

ll k;

const int mod=1e9+7;

struct MAT{

    ll a[55][55];

};

MAT mul(MAT &a,MAT &b){

    MAT c;

    for(int i=1;i<=n;++i){

        for(int j=1;j<=n;++j){

            c.a[i][j]=0;

            for(int k=1;k<=n;++k)(c.a[i][j]+=a.a[i][k]*b.a[k][j])%=mod;

        }

    }

    return c;

}

int main(){

    cin>>n>>k;

    MAT x,ans;

    memset(ans.a,0,sizeof(ans.a));

    for(int i=1;i<=n;++i){

        for(int j=1;j<=n;++j)cin>>x.a[i][j];

        ans.a[i][i]=1;

    }

    while(k){

        if(k&1)ans=mul(x,ans);

        k>>=1,x=mul(x,x);

    }

    ll out=0;

    for(int i=1;i<=n;++i){

        for(int j=1;j<=n;++j)out+=ans.a[i][j];

    }

    cout<<out%mod<<endl;

	//system("pause");

	return 0;

}