// 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: 4.547526736223328x):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

int n;

vector<vector<ll> >vec;

ll k,ans;

const ll M=1e9+7;

vector<vector<ll> >dp,tmp1,tmp2;

void go()

{

    tmp1=dp;

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

	{

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

		{

            dp[i][j]=0;

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

                dp[i][j]=(dp[i][j]+(tmp1[i][k]*tmp1[k][j]))%M;

        }

    }

}

void odd()

{

    tmp1=dp;

    tmp2=vec;

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

	{

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

		{

            dp[i][j]=0;

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

                dp[i][j]=(dp[i][j]+(tmp1[i][k]*tmp2[k][j]))%M;

        }

    }

}

void solve(ll k)

{

    if(k==1)return;

    solve(k/2);

    go();

    if(k%2)odd();

    return;

}

int main()

{

    cin>>n>>k;

    vec.resize(n);

    dp.resize(n);

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

	{

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

		{

            int x;

            cin>>x;

            vec[i].push_back(x);

            dp[i].push_back(x);

        }

    }

    solve(k);

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

        ans+=(dp[i][j]%M);

    cout<<ans%M<<endl;

    return 0;

}