#include<iostream>
#include<vector>
#include<string>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= end; --i)
#define iter(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define riter(i, c) for (auto i = (c).rbegin(); i != (c).rend(); ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;

template<typename T, long long MOD_VALUE>
class ModInt {
    static constexpr long long MOD = MOD_VALUE;
    private:
        T value_;
    public:
        ModInt() {}
        ModInt(const T& value):value_(value % MOD) {}
        ModInt& operator+=(const ModInt& x)  {value_ += x.value_; if (value_ >= MOD) value_ -= MOD; return *this;}
        friend ModInt& operator+=(const T& x, const ModInt& y) {ModInt res(x); res.value_ += x.value_; if (res.value_ >= MOD) res.value_ -= MOD; return res;}
        ModInt& operator-=(const ModInt& x) {if (value_ < x.value_) value_ += MOD; value_ -= x.value_; return *this;}
        friend ModInt& operator-=(const T& x, const ModInt& y) {ModInt res(x); if (res.value_ < y.value_) res.value_ += MOD; res.value_ -= y.value_; return res;}
        ModInt& operator*=(const ModInt& x) {value_ = (value_ * x.value_) % MOD; return *this;}
        friend ModInt& operator*=(const T& x, const ModInt& y) {ModInt res(x); res.value_ = (res.value_ * y.value_) % MOD; return res;}
        const ModInt operator+(const ModInt& x) const {return ModInt(*this) += x;}
        friend const ModInt operator+(const T& x, const ModInt& y) {return ModInt(x) += y;}
        const ModInt operator-(const ModInt& x) const {return ModInt(*this) -= x;}
        friend const ModInt operator-(const T& x, const ModInt& y) {return ModInt(x) -= y;}
        const ModInt operator*(const ModInt& x) const {return ModInt(*this) *= x;}
        friend const ModInt operator*(const T& x, const ModInt& y) {return ModInt(x) *= y;}
        static ModInt modpow(ModInt x, long long y) {ModInt z = 1; while (y > 0) {if (y & 1) {z *= x;}x *= x; y /= 2;} return z;}
        ModInt& operator/=(const ModInt& x) {return *this *= modpow(x, MOD - 2);}
        const ModInt operator/(const ModInt& x) const {return ModInt(*this) /= x;}
        friend const ModInt operator/(const T& x, const ModInt& y) {return ModInt(x) /= y;}
        ModInt operator++(int) {ModInt tmp(*this); value_ = (value_ + 1 == MOD ? 0 : value_ + 1); return tmp;}
        ModInt operator--(int) {ModInt tmp(*this); value_ = (value_ - 1 < 0 ? MOD - 1 : value_ - 1); return tmp;}
        friend istream& operator>>(istream& stream, ModInt& x) {stream >> x.value_; x.value_ %= MOD; return stream;}
        friend ostream& operator<<(ostream& stream, const ModInt& x) {stream << x.value_; return stream;}
};

using mint = ModInt<ll, 1000000007>;

void solve1() {
    // 自分の解法. ダブリングを用いている.
    ll N, K;
    cin >> N >> K;
    vector<vector<int>> A(N, vector<int>(N, 0));
    rep(i, 0, N) rep(j, 0, N) {
        cin >> A[i][j];
    }
    ll pow = 1;
    ll cnt = 0;
    while (pow < K) {
        pow *= 2;
        ++cnt;
    }
    vector<vector<vector<mint>>> dp(N, vector<vector<mint>>(N, vector<mint>(cnt + 1, 0)));  // dp[i][j][k]: 頂点iから頂点jまでのパスの長さが2^kとなる通り数
    rep(i, 0, N) rep(j, 0, N) {
        if (A[i][j]) {
            dp[i][j][0] = 1;
        }
    }
    rep(l, 1, cnt + 1) rep(i, 0, N) rep(j, 0, N) rep(k, 0, N) {
        // 頂点iから頂点jまで頂点kを経由して距離2^lとなるパスの通り数
        dp[i][j][l] += dp[i][k][l - 1] * dp[k][j][l - 1];
    }
    vector<vector<mint>> dp1(N, vector<mint>(N));  // dp1[i][j]: 頂点iから頂点jまでの通り数
    int idx = 0;
    rep(i, 0, 60) {
        if ((K >> i) & 1) {
            idx = i;
            rep(j, 0, N) rep(k, 0, N) {
                dp1[j][k] = dp[j][k][i];
            }
            break;
        }
    }
    rep(i, idx + 1, 60) {
        if ((K >> i) & 1) {
            vector<vector<mint>> dp2(N, vector<mint>(N, 0));
            rep(j, 0, N) rep(k, 0, N) rep(l, 0, N) {
                dp2[j][k] += dp1[j][l] * dp[l][k][i];
            }
            dp1 = dp2;
        }
    }
    mint ans = 0;
    rep(i, 0, N) rep(j, 0, N) {
        ans += dp1[i][j];
    }
    cout << ans << endl;
    return;
}

const ll MOD = 1e9 + 7;

template<typename T>
class Matrix {
    private:
        using array = vector<T>;
        using matrix = vector<array>;
        size_t row_size, column_size;
        matrix data;
    public:
        Matrix() {}
        Matrix(size_t _row_size, size_t _column_size):data(_row_size, array(_column_size, T())), row_size(_row_size), column_size(_column_size) {}
        Matrix(const Matrix<T>& matrix):data(matrix.data), row_size(matrix.row_size), column_size(matrix.column_size){}

        size_t row() const {return row_size;}
        size_t col() const {return column_size;}

        array& operator[](size_t idx) {
            return data[idx];
        }
        const array& operator[](size_t idx) const {
            return data[idx];
        }
        Matrix& operator+=(const Matrix& X) {
            for (int i = 0; i < (int)row_size; i++) {
                for (int j = 0; j < (int)column_size; j++) {
                    data[i][j] += X[i][j];
                }
            }
            return *this;
        }
        Matrix& operator-=(const Matrix& X) {
            for (int i = 0; i < (int)row_size; i++) {
                for (int j = 0; j < (int)column_size; j++) {
                    data[i][j] -= X[i][j];
                }
            }
            return *this;
        }
        Matrix operator*(const Matrix& X) const {
            size_t new_row = row_size, new_column = X.col();
            Matrix res(new_row, new_column);
            for (int i = 0; i < (int)res.row(); i++) {
                for (int j = 0; j < (int)res.col(); j++) {
                    for (int k = 0; k < (int)column_size; k++) {
                        res[i][j] = (res[i][j] + (data[i][k] * X[k][j]) % MOD) % MOD;
                    }
                }
            }
            return res;
        }
        Matrix& operator*=(T scalar) {
            for (int i = 0; i < (int)row_size; i++) {
                for (int j = 0; j < (int)column_size; j++) {
                    data[i][j] *= scalar;
                }
            }
            return *this;
        }
        Matrix& operator%=(T modulus) {
            for (int i = 0; i < (int)row_size; i++) {
                for (int j = 0; j < (int)column_size; j++) {
                    data[i][j] %= modulus;
                }
            }
            return *this;
        }
        Matrix operator+(const Matrix& X) const {
            return Matrix(*this) += X;
        }
        Matrix operator-(const Matrix& X) const {
            return Matrix(*this) -= X;
        }
        
        Matrix operator*(T scalar) const {
            return Matrix(*this) *= scalar;
        }
        Matrix operator%(T modulus) const {
            return Matrix(*this) %= modulus;
        }

        static Matrix identity(size_t row, size_t col) {
            Matrix<T> res(row, col);
            for (int i = 0; i < (int)res.row(); i++) {
                res[i][i] = 1;
            }
            return res;
        }
};

Matrix<ll> calc_pow(Matrix<ll> x, ll y) {
    Matrix<ll> z = Matrix<ll>::identity(x.row(), x.col());
    while (y > 0) {
        if (y & 1) {
            z = z * x;
        }
        x = x * x;
        y /= 2;
    }
    return z;
}

void solve2() {
    // 想定解. 行列累乗を用いる.
    ll N, K;
    cin >> N >> K;
    Matrix<ll> A(N, N);
    ll a;
    rep(i, 0, N) rep(j, 0, N) {
        cin >> a;
        A[i][j] = a;
    }
    Matrix<ll> pow_a = calc_pow(A, K);
    mint ans = 0;
    rep(i, 0, N) rep(j, 0, N) {
        ans += pow_a[i][j];
    }
    cout << ans << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // solve1();
    solve2();
    return 0;
}

Measured memory_bytes: 384