/*

参考:
https://ei1333.github.io/luzhiled/snippets/math/matrix.html
verify:
https://atcoder.jp/contests/dp/tasks/dp_r
*/

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

using ll=long long int;

constexpr ll MOD=1e9+7;
//__builtin_popcount(S);
//#define int ll



template<typename T>
struct Matrix{
    vector<vector<T>> x;

    Matrix(int h,int w):x(h,vector<T>(w)){}
    Matrix(const vector<vector<T>> &x):x(x){}

    inline int height()const{return (int)x.size();}

    inline int width()const{return (int)x[0].size();}

    inline vector<T> &operator[](int k){
        return x[k];
    }
    inline const vector<T> &operator[](int k)const{
        return x[k];
    }
    //O(hw)
    inline Matrix &operator+=(const Matrix &rhs)noexcept{
        int h=height(),w=width();
        assert(h==rhs.height() && w==rhs.width());
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++) x[i][j]+=rhs[i][j];
        }
        return (*this);
    }
    //O(hw) rhsに変更なし
    inline Matrix &operator-=(const Matrix &rhs)noexcept{
        int h=height(),w=width();
        assert(h==rhs.height() && w==rhs.width());
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++) x[i][j]-=rhs[i][j];
        }
        return (*this);
    }
    //O(hw*rw) rhsに変更なし
    inline Matrix &operator*=(const Matrix &rhs)noexcept{
        x=((*this)*rhs).x;
        return (*this);
    }
    inline Matrix operator+(const Matrix &rhs)const noexcept{
        return Matrix(*this)+=rhs;
    }
    inline Matrix operator-(const Matrix &rhs)const noexcept{
        return Matrix(*this)-=rhs;
    }
    //O(hw*rw)
    inline Matrix operator*(const Matrix &rhs)const noexcept{
        int h=height(),w=width(),rw=rhs.width();
        assert(w==rhs.height());
        vector<vector<T>> res(h,vector<T>(rw,0));
        
        for(int i=0;i<h;i++){
            for(int j=0;j<rw;j++){
                for(int k=0;k<w;k++){
                    res[i][j]+=x[i][k]*rhs[k][j];
                    res[i][j]%=MOD;
                }
            }
        }
        return res;
    }
    //O(N^3logk)
    inline Matrix pow(ll k)const noexcept{
        assert(height()==width());
        Matrix res(height(),width()),t(x);
        bool flag=true;
        while(k>0){
            if(k&1){
                if(flag) res=t,flag=false;
                else res*=t;
            }
            t*=t;
            k>>=1;
        }
        return res;
    }
    //inline Matrix
    friend ostream &operator<<(ostream &os,const Matrix &m){
        int h=m.height(),w=m.width();
        os<<endl;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(j) os<<',';
                os<<m[i][j];
            }
            os<<endl;
        }
        return os;
    }


};

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr); 
    cout<<fixed<<setprecision(20);
    int N;
    ll K;
    cin>>N>>K;
    vector<vector<ll>> A(N,vector<ll>(N,0));
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++) cin>>A[i][j];
    }
    Matrix<ll> M(A);
    auto ret=M.pow(K);
    ll sum=0;
    //cout<<ret;
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++) sum+=ret[i][j];
        sum%=MOD;
    }
    cout<<sum<<endl;
}

Measured memory_bytes: 384