#include<bits/stdc++.h>
#define int long long
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

template<class T> struct Matrix
{
    vector<vector<T>> A;
    Matrix(){}
    Matrix(int n,int m):A(n,vector<T>(m,0)){}
    Matrix(int n):A(n,vector<T>(n,0)){};
    int height() const{
        return (A.size());
    }
    int width() const{
        return (A[0].size());
    }
    inline const vector<T> &operator[](int k) const{
        return (A.at(k));
    }
    inline vector<T> &operator[](int k){
        return (A.at(k));
    }
    static Matrix I(int n){
        Matrix mat(n);
        for(int i=0;i<n;i++) mat[i][i]=1;
        return (mat);
    }
    Matrix &operator+=(const Matrix &B){
        int n=height(), m=width();
        assert(n==B.height() && m==B.width());
        for(int i=0;i<n;i++) for(int j=0;j<m;j++){
            (*this)[i][j]+=B[i][j];
        }
        return (*this);
    }
    Matrix &operator-=(const Matrix &B){
        int n=height(), m=width();
        assert(n==B.height() && m==B.width());
        for(int i=0;i<n;i++) for(int j=0;j<m;j++){
            (*this)[i][j]-=B[i][j];
        }
        return (*this);
    }
    Matrix &operator*=(const Matrix &B){
        int n=height(), m=B.width(), p=width();
        assert(p==B.height());
        vector<vector<T>> C(n,vector<T>(m,0));
        for(int i=0;i<n;i++) for(int j=0;j<m;j++){
            for(int k=0;k<p;k++){
                C[i][j]=(C[i][j]+(*this)[i][k]*B[k][j]);
            }
        }
        A.swap(C);
        return (*this);
    }
    Matrix &operator^=(int k){
        Matrix B=Matrix::I(height());
        while(k>0){
            if(k&1) B*=*this;
            *this*=*this;
            k>>=1;
        }
        A.swap(B.A);
        return (*this);
    }
    Matrix operator+(const Matrix &B) const{
        return (Matrix(*this)+=B);
    }
    Matrix operator-(const Matrix &B) const{
        return (Matrix(*this)-=B);
    }
    Matrix operator*(const Matrix &B) const{
        return (Matrix(*this)*=B);
    }
    Matrix operator^(const int k) const{
        return (Matrix(*this)^=k);
    }
};

template<int mod> class modint
{   
private:
    int val;
public:
    int value(){ return val; }
    modint(int x=0){ val=x%mod; }
    modint pow(int n){
        modint res(1),x(val);
        while(n>0){ if(n&1) res*=x; x*=x; n>>=1; }
        return res;
    }
    modint inv(){ return pow(mod-2); }
    modint& operator+=(modint rhs){ val+=rhs.val; if(val>=mod) val-=mod; return *this; }
    modint& operator-=(modint rhs){ val+=mod-rhs.val; if(val>=mod) val-=mod; return *this; }
    modint& operator*=(modint rhs){ val=val*rhs.val%mod; return *this; }
    modint& operator/=(modint rhs){ *this*=rhs.inv(); return *this; }
    modint operator+(modint rhs){ return modint(val)+=rhs; }
    modint operator-(modint rhs){ return modint(val)-=rhs; }
    modint operator*(modint rhs){ return modint(val)*=rhs; }
    modint operator/(modint rhs){ return modint(val)/=rhs; }
};

signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    using mint=modint<1000000007>;
    int N,K; cin>>N>>K;
    Matrix<mint> A(N);
    for(int i=0;i<N;i++) for(int j=0;j<N;j++){
        int t; cin>>t;
        A[i][j]=t;
    }
    A^=K;
    mint ans=0;
    for(int i=0;i<N;i++) for(int j=0;j<N;j++) ans+=A[i][j];
    cout<<ans.value()<<endl;
    return 0;
}

Measured memory_bytes: 384