#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ASSERT(C, ...) assert( (C) || (PRINT(__VA_ARGS__), false) )
template <class Head> void PRINT(Head head) {
    std::cout << head << "\n";
}
template <class Head, class... Tail> void PRINT(Head head, Tail... tail) {
    std::cout << head << " ";
    PRINT(tail...);
}

template<typename T> struct Matrix {
    int row;
    int col;
    vector<vector<T>> mat;
    Matrix(const int& row, const int& col) : row(row), col(col) {
        mat.assign(row, vector<T>(col, T(0)));
    }
    Matrix(const int& row, const int& col, function<T(int,int)> f) : row(row), col(col) {
        mat.resize(row);
        for ( int r = 0; r < row; r++ ) {
            mat[r].resize(col);
        }
        for ( int r = 0; r < row; r++ ) {
            for ( int c = 0; c < col; c++ ) {
                mat[r][c] = f(r,c);
            }
        }
    }
    Matrix(const Matrix& m) {
        row = m.row;
        col = m.col;
        mat.resize(row);
        for ( int r = 0; r < row; r++ ) {
            mat[r].resize(col);
        }
        for ( int r = 0; r < row; r++ ) {
            for ( int c = 0; c < col; c++ ) {
                mat[r][c] = m.mat[r][c];
            }
        }
    }
    static Matrix<T> CreateIdentity(const int& n) {
        Matrix<T> m(n, n, [](int r, int c) {return r==c ? T(1) : T(0);});
        return m;
    };    
    inline T& operator()(unsigned int r, unsigned int c) {
        return mat[r][c];
    }
    inline vector<T>& operator[](unsigned int r) {
        return mat[r];
    }
    Matrix<T>& operator=(const Matrix& o) {
        if ( row != o.row || col != o.col ) {
            row = o.row;
            col = o.col;
            mat.resize(row);
            for ( int r = 0; r < row; r++ ) {
                mat[r].resize(col);
            }
        }
        for ( int r = 0; r < row; r++ ) {
            for ( int c = 0; c < col; c++ ) {
                mat[r][c] = o.mat[r][c];
            }
        }
        return *this;
    }
    bool operator==(const Matrix& o) {
        if ( row != o.row ) return false;
        if ( col != o.col ) return false;
        for ( int r = 0; r < row; r++ ) {
            for ( int c = 0; c < col; c++ ) {
                if ( mat[r][c] != o.mat[r][c] ) return false;
            }
        }
        return true;
    }
    bool operator!=(const Matrix& o) {
        return !this->operator==(o);
    }    
    Matrix<T>& operator+=(const Matrix& o) {
        ASSERT ( row == o.row, "row size error", row, o.row);
        ASSERT ( col == o.col, "col size error", col, o.col);
        for ( int r = 0; r < row; r++ ) {
            for ( int c = 0; c < col; c++ ) {
                mat[r][c] += o.mat[r][c];
            }
        }
        return *this;
    }
    Matrix<T>& operator-=(const Matrix& o) {
        ASSERT ( row == o.row, "row size error", row, o.row);
        ASSERT ( col == o.col, "col size error", col, o.col);
        for ( int r = 0; r < row; r++ ) {
            for ( int c = 0; c < col; c++ ) {
                mat[r][c] -= o.mat[r][c];
            }
        }
        return *this;
    }
    Matrix<T>& operator*=(const T& x) {
        for ( int r = 0; r < row; r++ ) {
            for ( int c = 0; c < col; c++ ) {
                mat[r][c] *= x;
            }
        }
        return *this;
    }
    Matrix<T> operator+(const Matrix& o) {
        Matrix<T> c(*this);
        c += o;
        return c;
    }
    Matrix<T> operator-(const Matrix& o) {
        Matrix<T> c(*this);
        c -= o;
        return c;
    }
    Matrix<T> operator*(const Matrix& o) {
        Matrix<T> ret(this->row, o.col);
        for ( int r = 0; r < ret.row; r++ ) {
            for ( int c = 0; c < ret.col; c++ ) {
                for ( int k = 0; k < o.row; k++ ) {
                    ret.mat[r][c] += mat[r][k] * o.mat[k][c];
                }
            }
        }
        return ret;
    }
    Matrix<T> pow(long long n) {
        Matrix<T> t(*this), ret = Matrix<T>::CreateIdentity(this->row);
        while ( n > 0 ) {
            if ( n & 1 ) ret = ret * t;
            t = t * t;
            n >>= 1;
        }
        return ret;
    }
};


const ll MOD = 1e9 + 7;
template<ll MOD> struct ModInt {
    ll value;
    ModInt() : value(0) {}
    ModInt(const ll& v) : value(v % MOD) {
        if ( value < 0 ) value += MOD;
    }
    ModInt<MOD>& operator=(const ModInt& o) {
        value = o.value;
        return *this;
    }
    inline bool operator==(const ModInt& o) {
        return value == o.value;
    }
    inline bool operator!=(const ModInt& o) {
        return value != o.value;
    }
    ModInt<MOD>& operator+=(const ModInt& o) {
        value += o.value;
        if ( value >= MOD ) value -= MOD;
        return *this;
    }
    ModInt<MOD>& operator-=(const ModInt& o) {
        value += MOD - o.value;
        if (value >= MOD ) value -= MOD;
        return *this;
    }
    ModInt<MOD>& operator*=(const ModInt& o) {
        value *= o.value;
        value %= MOD;
        return *this;
    }
    ModInt<MOD>& operator/=(const ModInt& o) {
        value *= inverse(o.value);
        value %= MOD;
        return *this;
    }
    ModInt<MOD> operator-() {
        return ModInt<MOD>(-value);
    }
    ModInt<MOD> operator+(const ModInt& o) {
        ModInt<MOD> r(*this);
        r += o;
        return r;
    }
    ModInt<MOD> operator-(const ModInt& o) {
        ModInt<MOD> r(*this);
        r -= o;
        return r;
    }
    ModInt<MOD> operator*(const ModInt& o) {
        ModInt<MOD> r(*this);
        r *= o;
        return r;
    }
    ModInt<MOD> operator/(const ModInt& o) {
        ModInt<MOD> r(*this);
        r /= o;
        return r;
    }
    ModInt<MOD> pow(ll n) {
        ModInt<MOD> t(*this), r = 1;
        while ( n > 0 ) {
            if ( n & 1 ) r *= t;
            t *= t;
            n >>= 1;
        }
        return r;
    }
private:
    ll inverse(ll a) {
        ModInt<MOD> t(a);
        return t.pow(MOD - 2).value;
    }
};
using Int = ModInt<MOD>;

ll solve() {
    ll N, K, a;
    cin >> N >> K;
    Matrix<Int> A(N,N);
    for ( int i = 0; i < N; i++ ) {
        for ( int j = 0; j < N; j++ ) {
            cin >> a;
            A(i,j) = a;
        }
    }
    auto m = A.pow(K);
    Int ans = 0;
    for ( int i = 0; i < N; i++ ) {
        for ( int j = 0; j < N; j++ ) {
            ans += m(i,j);
        }
    }
    return ans.value;
}

int main() {
    auto ans = solve();
    cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 384