#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define all(a) a.begin(), a.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;

template<const long long mod = 1000000007>
struct mint{ //modint
    long long x;
    mint(long long x = 0): x(((x % mod) + mod) % mod) {}
    friend istream& operator >> (istream &s, mint& m){ return s >> m.x; }
    friend ostream& operator << (ostream &s, mint m){ return s << m.x; }
    inline mint& operator += (mint a){ if((x += a.x) >= mod)x -= mod; return *this; }
    inline mint& operator -= (mint a){ if((x += mod - a.x) >= mod)x -= mod; return *this; }
    inline mint& operator *= (mint a){ (x *= a.x) %= mod; return *this; }
    inline mint& operator /= (mint a){ return (*this) *= a.inv(); }
    inline mint operator + (mint a){ mint res(*this); return res += a; }
    inline mint operator - (mint a){ mint res(*this); return res -= a; }
    inline mint operator * (mint a){ mint res(*this); return res *= a; }
    inline mint operator / (mint a){ mint res(*this); return res /= a; }
    inline void takemod(){ x = ((x % mod) + mod) % mod; }
    mint inv(){ return pow(mod - 2); }
    mint pow(long long p){
        mint res(1), tmp(x);
        for(; p; p >>= 1){ if(p & 1)res *= tmp; tmp *= tmp; }
        return res;
    }
};

template<typename T>
struct Matrix{
public:
    int Hei, Wid;
    vector<vector<T>> Mat;
    Matrix(vector<vector<T>> v) : Hei((int)v.size()), Wid((int)v[0].size()), Mat(v) {}
    Matrix(int h, int w) : Hei(h), Wid(w), Mat(vector<vector<T>>(h, vector<T>(w))) {}
    inline vector<T>& operator [] (int i){ return Mat[i]; }
    inline Matrix operator + (Matrix Mat2){ Matrix res = *this; return res += Mat2; }
    inline Matrix& operator += (Matrix Mat2){
        assert(Hei == Mat2.Hei && Wid == Mat2.Wid);
        rep(i, Hei)rep(j, Wid)Mat[i][j] += Mat2[i][j];
        return *this;
    }
    inline Matrix operator - (Matrix Mat2){ Matrix res = *this; return res -= Mat2; }
    inline Matrix& operator -= (Matrix Mat2){
        assert(Hei == Mat2.Hei && Wid == Mat2.Wid);
        rep(i, Hei)rep(j, Wid)Mat[i][j] -= Mat2[i][j];
        return *this;
    }
    inline Matrix operator * (Matrix Mat2){ Matrix res = *this; return res *= Mat2; }    
    inline Matrix& operator *= (Matrix Mat2){
        assert(Wid == Mat2.Hei);
        Matrix res(Hei, Mat2.Wid);
        rep(i, Hei)rep(j, Mat2.Wid)rep(k, Wid){
            res[i][j] += Mat[i][k] * Mat2[k][j];
        }
        return *this = res;
    }
    inline Matrix operator * (T scalar){ Matrix res = *this; return res *= scalar; }
    inline Matrix operator *= (T scalar){
        rep(i, Hei)rep(j, Wid)Mat[i][j] *= scalar;
        return *this;
    }
    inline Matrix pow(long long pow){
        assert(Hei == Wid);
        Matrix res(Hei, Wid), tmp = *this;
        rep(i, Hei)res[i][i] = 1;
        for(; pow; pow >>= 1){ if(pow & 1)res *= tmp; tmp *= tmp; }
        return res;
    }
};


int main(){
    
    int n; long long k; cin >> n >> k;
    Matrix<mint<>> mat(n, n);
    rep(i, n)rep(j, n)cin >> mat[i][j];

    mint<> ans;
    Matrix<mint<>> power = mat.pow(k);
    rep(i, n)rep(j, n)ans += power[i][j];
    cout << ans << endl;

}

Measured memory_bytes: 384