#include <bits/stdc++.h>
using namespace std;


template<typename T>
using matrix = std::vector<std::vector<T>>;

template<typename T>
matrix<T> make_matrix(int n, int m, T id = 0) {
    return matrix<T>(n, std::vector<T>(m, id));
}

template<typename T>
matrix<T> operator-(matrix<T> m) {
    for(int i=0; i<m.size(); ++i) {
        for(int j=0; j<m[i].size(); ++j) {
            m[i][j] = -m[i][j];
        }
    }
    return m;
}

template<typename T>
matrix<T> operator+(matrix<T> lhs, matrix<T> const& rhs) {
    assert(lhs.size() == rhs.size() && lhs[0].size() == rhs[0].size());
    for(int i=0; i<lhs.size(); ++i) {
        for(int j=0; j<lhs[i].size(); ++j) {
            lhs[i][j] += rhs[i][j];
        }
    }
    return lhs;
}

template<typename T>
matrix<T> operator-(matrix<T> lhs, matrix<T> const& rhs) {
    assert(lhs.size() == rhs.size() && lhs[0].size() == rhs[0].size());
    for(int i=0; i<lhs.size(); ++i) {
        for(int j=0; j<lhs[i].size(); ++j) {
            lhs[i][j] -= rhs[i][j];
        }
    }
    return lhs;
}

template<typename T>
matrix<T> operator*(T lhs, matrix<T> const& rhs) {
    auto res = rhs;
    for(int i=0; i<res.size(); ++i) {
        for(int j=0; j<res[i].size(); ++j) {
            res[i][j] *= lhs;
        }
    }
    return res;
}

template<typename T>
matrix<T> operator*(matrix<T> a, std::vector<T> const& b) {
    const int n = a.size();
    const int m = b.size();
    std::vector<T> res(n);
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < m; ++j) {
            res[i] += a[i][j] * b[j];
        }
    }
    return res;
}

template<typename T>
matrix<T> operator*(matrix<T> const& lhs, matrix<T> const& rhs) {
    if(lhs.size() == 1 && lhs[0].size() == 1) {
        return lhs[0][0] * rhs;
    }
    if(rhs.size() == 1 && rhs[0].size() == 1) {
        return rhs[0][0] * lhs;
    }
    assert(lhs[0].size() == rhs.size());
    matrix<T> res(lhs.size(), std::vector<T>(rhs[0].size()));
    for(int i=0; i<(int)lhs.size(); ++i) {
        for(int k=0; k<(int)rhs.size(); ++k) {
            for(int j=0; j<(int)rhs[0].size(); ++j) {
                res[i][j] += lhs[i][k] * rhs[k][j];
            }
        }
    }
    return res;
}

template <typename T>
matrix<T> modmul(matrix<T> const& lhs, matrix<T> const& rhs, long long const M) {
    matrix<T> res(lhs.size(), std::vector<T>(rhs[0].size()));
    for(int i = 0; i < (int)lhs.size(); ++i) {
        for(int k = 0; k < (int)rhs.size(); ++k) {
            for(int j = 0; j < (int)rhs[0].size(); ++j) {
                res[i][j] += lhs[i][k] * rhs[k][j] % M;
                res[i][j] %= M;
            }
        }
    }
    return res;
}

template <typename T>
std::vector<T> modmul(matrix<T> const& a, std::vector<T> const& b, long long const M) {
    const int n = a.size();
    const int m = b.size();
    std::vector<T> res(n);
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < m; ++j) {
            res[i] = res[i] + (a[i][j] * b[j]) % M;
            res[i] %= M;
        }
    }
    return res;
}

template <typename T>
matrix<T> transpose(matrix<T> const& m) {
    const int R = m.size(), C = m[0].size();
    matrix<T> res(C, std::vector<T>(R));
    for(int i=0; i<C; ++i) {
        for(int j=0; j<R; ++j) {
            res[i][j] = m[j][i];
        }
    }
    return res;
}

template<typename T>
matrix<T> eye(int n) {
    matrix<T> res(n, std::vector<T>(n));
    for(int i = 0; i < n; ++i) {
        res[i][i] = 1;
    }
    return res;
}

template <typename T>
matrix<T> matpow(matrix<T> x, long long n) {
    auto res = eye<T>(x.size());
    while(n > 0) {
        if(n & 1) {
            res = res * x;
        }
        x = x * x;
        n >>= 1;
    }
    return res;
}

template<typename T>
void print(matrix<T> const& m) {
    for(int i=0; i<m.size(); ++i) {
        for(int j=0; j<m[i].size(); ++j) {
            if(j != 0) {
                cout << ' ';
            }
            cout << (T)m[i][j];
        }
        cout << endl;
    }
}


template <int Mod, bool IsPrime = false>
class mod_int {
    using ll = long long;

public:
    constexpr mod_int() : n(0) {}
    constexpr mod_int(int n_) : n(n_) {
        if(n >= Mod)     n %= Mod;
        else if(n < 0) n = (n % Mod + Mod) % Mod;
    }
    constexpr mod_int(ll n_) : n(n_) { n = (n + Mod) % Mod; }

    constexpr operator int() const { return n; }
    constexpr operator ll() const  { return n; }

    constexpr bool operator==(mod_int const& other) const { return n == other.n; }
    constexpr mod_int& operator+=(mod_int const& other) {
        if((n += other.n) >= Mod) n -= Mod;
        return *this;
    }
    constexpr mod_int& operator-=(mod_int const& other) {
        if((n += Mod - other.n) >= Mod) n -= Mod;
        return *this;
    }
    constexpr mod_int& operator*=(mod_int const& other) {
        n = (unsigned long long)n * other.n % Mod;
        return *this;
    }
    constexpr typename std::enable_if<IsPrime, mod_int>::type& operator/=(mod_int const& other) {
        return *this *= other.inverse();
    }
    constexpr mod_int operator+(mod_int other) const { return mod_int(*this) += other; }
    constexpr mod_int operator-(mod_int other) const { return mod_int(*this) -= other; }
    constexpr mod_int operator*(mod_int other) const { return mod_int(*this) *= other; }
    constexpr mod_int operator/(mod_int other) const { return mod_int(*this) /= other; }

    constexpr typename std::enable_if<IsPrime, mod_int>::type inverse() const {
        ll a = n, b = Mod, u = 1, v = 0;
        while(b) {
            ll t = a / b;
            a -= t * b; std::swap(a, b);
            u -= t * v; std::swap(u, v);
        }
        return mod_int(u);
    }

private:
    ll n;
};

template <int Mod, bool IsPrime>
std::ostream& operator<<(std::ostream& os, mod_int<Mod, IsPrime> const& n) {
    os << (int)n;
    return os;
}

constexpr int default_mod = 1000000007;

template <int Mod = default_mod>
mod_int<Mod, true> fact(int n) {
    static std::vector<mod_int<Mod, true>> v = {1};
    if(n >= static_cast<int>(v.size())) {
        const int from = v.size(), to = n + 1024;
        v.reserve(to);
        for(int i = from; i < to; ++i) {
            v.push_back(v.back() * mod_int<Mod, true>(i));
        }
    }
    return v[n];
}

template <int Mod = default_mod>
mod_int<Mod, true> comb(int n, int r) { // nCr
    if(r < 0 || r > n) return 0;
    return fact<Mod>(n) / fact<Mod>(r) / fact<Mod>(n - r);
}

using mint = mod_int<default_mod, true>; // default

using ll = long long;

int main() {
    ll n, k; cin >> n >> k;
    matrix<mint> a(n, vector<mint>(n));
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            int x; cin >> x;
            a[i][j] = x;
        }
    }

    a = matpow(a, k);
    mint ans = 0;
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            ans += a[i][j];
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 384