/*

参考:
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;
    function<T(T,T)> plus;
    function<T(T,T)> mul;

    Matrix(){}

    Matrix(int h,int w,
        const function<T(T,T)> &plus=[](T a,T b){return a+b;},const function<T(T,T)> &mul=[](T a,T b){return a*b;})
            :x(h,vector<T>(w)),plus(plus),mul(mul){}

    Matrix(const vector<vector<T>> &x,
        const function<T(T,T)> &plus=[](T a,T b){return a+b;},const function<T(T,T)> &mul=[](T a,T b){return a*b;})
            :x(x),plus(plus),mul(mul){}

    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]=plus(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;
    }
    //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]=plus(res[i][j],mul(x[i][k],rhs[k][j]));
                }
            }
        }
        return res;
    }
    //O(N^3logk)
    inline Matrix pow(ll k)const noexcept{
        assert(height()==width());
        Matrix res(height(),width(),plus,mul),t(x,plus,mul);
        bool flag=true;
        while(k>0){
            if(k&1){
                if(flag) res.x=t.x,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;
    }


};

struct ModInt{
    using u64=uint_fast64_t;
    u64 a;
    constexpr ModInt() :a(0){}
    constexpr ModInt(ll x) :a((x>=0)?(x%MOD):(x%MOD+MOD) ) {}

    inline constexpr ModInt operator+(const ModInt rhs)const noexcept{
        return ModInt(*this)+=rhs;
    }
    inline constexpr ModInt operator-(const ModInt rhs)const noexcept{
        return ModInt(*this)-=rhs;
    }
    inline constexpr ModInt operator*(const ModInt rhs)const noexcept{
        return ModInt(*this)*=rhs;
    }
    inline constexpr ModInt operator/(const ModInt rhs)const noexcept{
        return ModInt(*this)/=rhs;
    }
    inline constexpr ModInt operator+(const ll rhs) const noexcept{
        return ModInt(*this)+=rhs;
    }
    inline constexpr ModInt operator-(const ll rhs)const noexcept{
        return ModInt(*this)-=rhs;
    }
    inline constexpr ModInt operator*(const ll rhs)const noexcept{
        return ModInt(*this)*=rhs;
    }
    inline constexpr ModInt operator/(const ll rhs)const noexcept{
        return ModInt(*this)/=rhs;
    }

    inline constexpr ModInt &operator+=(const ModInt rhs)noexcept{
        a+=rhs.a;
        if(a>=MOD) a-=MOD;
        return *this;
    }
    inline constexpr ModInt &operator-=(const ModInt rhs)noexcept{
        if(rhs.a>a) a+=MOD;
        a-=rhs.a;
        return *this;
    }
    inline constexpr ModInt &operator*=(const ModInt rhs)noexcept{
        a=(a*rhs.a)%MOD;
        return *this;
    }
    inline constexpr ModInt &operator/=(ModInt rhs)noexcept{
        a=(a*rhs.inverse().a)%MOD;
        return *this;
    }
    inline constexpr ModInt &operator+=(const ll rhs)noexcept{
        a+=rhs;
        if(a>=MOD) a-=MOD;
        return *this;
    }
    inline constexpr ModInt &operator-=(const ll rhs)noexcept{
        if(rhs>a) a+=MOD;
        a-=rhs;
        return *this;
    }
    inline constexpr ModInt &operator*=(const ll rhs)noexcept{
        a=(a*rhs)%MOD;
        return *this;
    }
    inline constexpr ModInt &operator/=(ll rhs)noexcept{
        a=(a*ModInt(rhs).inverse().a)%MOD;
        return *this;
    }

    inline constexpr ModInt operator=(ll x)noexcept{
        x%=MOD;
        if(x<0) x+=MOD;
        a=x;
        return *this;
    }

    inline constexpr bool operator==(const ModInt p)const noexcept{
        return a==p.a;
    }

    inline constexpr bool operator!=(const ModInt p)const noexcept{
        return a!=p.a;
    }

    inline constexpr ModInt pow(ll N) const noexcept{
        ModInt ans(1LL),p(a);
        while(N>0){
            if(N&1){
                ans*=p;
            }
            p*=p;
            N>>=1;
        }
        return ans;
    }
    inline constexpr ModInt inverse() const noexcept{
        return pow(MOD-2);
    }

};
inline constexpr ModInt operator+(const ll &a,const ModInt &b)noexcept{
    return ModInt(a)+=b;
}
inline constexpr ModInt operator-(const ll &a,const ModInt &b)noexcept{
    return ModInt(a)-=b;
}
inline constexpr ModInt operator*(const ll &a,const ModInt &b)noexcept{
    return ModInt(a)*=b;
}
inline constexpr ModInt operator/(const ll &a,const ModInt &b)noexcept{
    return ModInt(a)/=b;
}
//cout
inline ostream &operator<<(ostream &os,const ModInt &p) {
    return os<<p.a;
}

//cin
inline istream &operator>>(istream &is,ModInt &p) {
    ll t;
    is>>t;
    p=t;
    return is;
}




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

Measured memory_bytes: 384