#include <vector>
#include <cassert>

template<class T>
struct matrix {
    using value_type = T;
    using size_type = std::size_t;
private :
    size_type h, w;
    std::vector<std::vector<value_type>> mat;

public : 
    matrix () { }

    matrix (size_type n) : 
        h(n), w(n), mat(n, std::vector<value_type>(n, value_type())) { }

    matrix (size_type n, const value_type &x) : 
        h(n), w(n), mat(n, std::vector<value_type>(n, x)) { }

    matrix (size_type h, size_type w) : 
        h(h), w(w), mat(h, std::vector<value_type>(w, value_type())) { }

    matrix (size_type h, size_type w, const value_type &x) :
        h(h), w(w), mat(h, std::vector<value_type>(w, x)) { }

    const std::vector<value_type> &operator[] (size_type i) const {
        return mat[i];
    }

    std::vector<value_type> &operator[] (size_type i) {
        return mat[i];
    }

    matrix &operator+= (const matrix &rhs) {
        assert(h == rhs.h and w == rhs.w);
        for (size_type i = 0; i < h; i++) {
            for (size_type j = 0; j < w; j++) {
                (*this)[i][j] += rhs[i][j];
            }
        }
        return (*this);
    }

    matrix &operator-= (const matrix &rhs) {
        assert(h == rhs.h and w == rhs.w);
        for (size_type i = 0; i < h; i++) {
            for (size_type j = 0; j < w; j++) {
                (*this)[i][j] -= rhs[i][j];
            }
        }
        return (*this);
    }

    matrix &operator*= (const matrix &rhs) {
        assert(h == rhs.h);
        std::vector<std::vector<value_type>> buff(h, std::vector<value_type>(rhs.w));
        for (size_type i = 0; i < h; i++) {
            for (size_type k = 0; k < rhs.h; k++) {
                for (size_type j = 0; j < rhs.w; j++) {
                    buff[i][j] += (mat[i][k] * rhs[k][j]);
                }
            }
        }
        mat.swap(buff);
        return (*this);
    }

    matrix &operator^= (long long x) {
        assert(h == w);
        matrix<value_type> buff(h);
        for (size_type i = 0; i < h; i++) {
            buff[i][i] = value_type(1);
        }
        while (x) {
            if (x & 1) buff *= (*this);
            (*this) *= (*this);
            x >>= 1;
        }
        mat.swap(buff.mat);
        return (*this);
    }

    matrix &operator+ (const matrix &rhs) {
        return ((*this) += rhs);
    }

    matrix &operator- (const matrix &rhs) {
        return ((*this) -= rhs);
    }

    matrix &operator* (const matrix &rhs) {
        return ((*this) *= rhs);
    }

    matrix &operator^ (long long x) {
        return ((*this) ^= x);
    }

};



#include <iostream>

template<int mod>
struct modint {
    int x;
    modint() : x(0) {}
    modint(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
    modint &operator+=(const modint &p) { if((x += p.x) >= mod) x -= mod; return *this; }
    modint &operator-=(const modint &p) { if((x += mod-p.x) >= mod) x -= mod; return *this; }
    modint &operator*=(const modint &p) { x = (int)(1LL*x*p.x%mod); return *this; }
    modint &operator/=(const modint &p) { *this *= p.inverse(); return *this; }
    modint operator-() const { return modint(-x); }
    modint operator+(const modint &p) const { return modint(*this) += p; }
    modint operator-(const modint &p) const { return modint(*this) -= p; }
    modint operator*(const modint &p) const { return modint(*this) *= p; }
    modint operator/(const modint &p) const { return modint(*this) /= p; }
    bool operator==(const modint &p) const { return x == p.x; }
    bool operator!=(const modint &p) const { return x != p.x; }
    modint inverse() const {
        int a = x, b = mod, u = 1, v = 0, t;
        while(b > 0) {
			t = a / b;
			a -= t * b;
			std::swap(a, b);
			u -= t * v;
			std::swap(u, v);
		}
        return modint(u);
    }
    modint pow(long long e){
        long long a = 1, p = x;
        while(e > 0) {
            if (e & 1) {a = (a * p) % mod; e--;}
            else {p = (p * p) % mod; e /= 2;}
        }
        return modint(a);
    }
    friend std::ostream &operator<<(std::ostream &os, const modint<mod> &p) {
        return os << p.x;
    }
    friend std::istream &operator>>(std::istream &is, modint<mod> &a) {
        long long x;
        is >> x;
        a = modint<mod>(x);
        return (is);
    }
};

constexpr int MOD = 1'000'000'007; //'

int main() {
    std::size_t n;
    long long k;
    std::cin >> n >> k;
    matrix<modint<MOD>> mat(n);
    for (std::size_t i = 0; i < n; i++) {
        for (std::size_t j = 0; j < n; j++) {
            std::cin >> mat[i][j];
        }
    }

    mat ^= k;

    modint<MOD> ans = 0;
    for (std::size_t i = 0; i < n; i++) {
        for (std::size_t j = 0; j < n; j++) {
            ans += mat[i][j];
        }
    }

    std::cout << ans << '\n';

    return 0;
}

Measured memory_bytes: 3668