#include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
template<class F>auto recursive(F f){return[f](auto...a){return f(f,a...);};}
template<class T>using rev_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<class T>using vector2d=vector<vector<T>>;
struct fixprec{int p;fixprec(int p):p(p){}};
ostream&operator<<(ostream&o,fixprec f){return o<<fixed<<setprecision(f.p);}

/*!* [modint] *!*/
/*+* 剰余環整数と剰余階乗 *+*/
struct int_m;
int_m invmod(int_m);
int_m powmod(int_m, i64);
struct int_m {
    static i64 m;
    i64 v;
    int_m() {}
    int_m(i64 x) : v(x) {}
    int_m operator+(const int_m &y) const { return (v + y.v) % m; }
    int_m operator-(const int_m &y) const { return (v - y.v + m) % m; }
    int_m operator*(const int_m &y) const { return (v * y.v) % m; }
    int_m operator/(const int_m &y) const { return *this * invmod(y); }
    int_m operator^(const i64   &y) const { return powmod(*this, y); }
    void operator+=(const int_m &y) { *this = *this + y; }
    void operator-=(const int_m &y) { *this = *this - y; }
    void operator*=(const int_m &y) { *this = *this * y; }
    void operator/=(const int_m &y) { *this = *this / y; }
    void operator^=(const i64   &y) { *this = *this ^ y; }
    bool operator==(const int_m &y) const { return v == y.v; }
    bool operator!=(const int_m &y) const { return v != y.v; }
    friend istream &operator>>(istream &ist, int_m &m) { ist >> m.v; return ist; }
    friend ostream &operator<<(ostream &ost, int_m m) { ost << m.v; return ost; }
};
i64 int_m::m;
// O(log a)
int_m powmod(int_m a, i64 p) {
    int_m v = 1;
    for(int_m b = a; p > 0; p >>= 1, b *= b) if(p & 1) v *= b;
    return v;
}
int_m invmod(int_m x) {
    return powmod(x, x.m - 2);
}

/*!* [linalg] *!*/
/*+* 線形代数 *+*/
// 行列
template <typename T, typename Container = vector2d<T>>
struct Matrix {
    int r, c;
    Container m;
    Matrix() {}
    // O(RC)
    Matrix(int row, int column = -1, T val = 0) {
        if (row <= 0) row = 0;
        if (column < 0) column = row;
        c = column, r = row;
        m.resize(r);
        for (int i = 0; i < m.size(); i++) {
            m[i].resize(c, val);
        }
    }
    // O(RC)
    Matrix(const Matrix<T> &rhs) {
        m = rhs.m;
        r = rhs.r;
        c = rhs.c;
    }

    ~Matrix() {}

    vector<T> &operator[](int i) {
        return m[i];
    }

    // O(ABC) AxB 型と BxC型 の積の場合
    Matrix<T> operator*(const Matrix<T> &x) {
        if (c != x.r) return I(0);
        Matrix<T> retval = Matrix<T>(r, x.c);
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < x.c; j++) {
                T tmp = 0;
                for (int k = 0; k < c; k++) {
                    tmp += m[i][k] * x.m[k][j];
                }
                retval[i][j] = tmp;
            }
        }
        return retval;
    }

    Matrix<T> operator*(const T &x) {
        Matrix<T> retval = Matrix<T>(r, c);
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                retval[i][j] *= x;
            }
        }
        return retval;
    }

    Matrix<T> operator+(const Matrix<T> &mat) {
        if (mat.r != mat || mat.c != c) return I(0);
        Matrix<T> retval = Matrix<T>(*this);
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                retval[i][j] += mat[i][j];
            }
        }
        return retval;
    }

    Matrix<T> operator-(const Matrix<T> &mat) {
        return (*this) + (mat * -1);
    }

    void power_(i64 p) {
        if (r != c) return;
        auto mul = *this;
        *this = I(r);
        for (; p > 0; p >>= 1, mul = mul * mul) {
            if (p & 1) {
                *this = mul * (*this);
            }
        }
    }

    static Matrix<T> I(int n) {
        Matrix<T> a(n, n, 0);
        for (int i = 0; i < n; i++) {
            a[i][i] = 1;
        }
        return a;
    }
};

int main() {
    int_m::m = 1e9 + 7;
    i64 N, K;
    cin >> N >> K;
    Matrix<int_m> mat(N);
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) {
            cin >> mat.m[i][j];
        }
    }
    mat.power_(K);
    int_m ans = 0;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) {
            ans += mat.m[i][j];
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 384