#ifdef _MSC_VER
#include "stdc++.h"
#endif
#ifdef __GNUC__
#include<bits/stdc++.h>
#endif
using namespace std;

using ll = long long;

//vector
using vi = vector<int>;
using vii = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;

//constant
const double EPS = 1e-10;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, s, e) for (ll i = s; i < e; i++)
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(), (x).rend()

//debug
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

// vector
//template<typename T> vector<vector<T>> vv(ll H, ll W, T v) { return vector<vector<T>> vec(H, vector<T>(W, v)); }

// view vector
template<typename T> void view(T e) { cout << e << endl; }
template<typename T> void view(const vector<T>& v) { for (const auto& e : v) { cout << e << " "; } cout << endl; }
template<typename T> void view(const vector<vector<T> >& vv) { for (const auto& v : vv) { view(v); } }

// DP
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

const ll inf = 1LL << 60;
const ll MOD = 1000000007;
struct Mint {
    ll v;
    ll _mod;
    bool init = false;
    Mint() : v(0), _mod(MOD) {}
    Mint(signed v, ll _mod = MOD) : v(v), _mod(_mod) {}
    Mint(ll t, ll _mod = MOD, bool init = false) : _mod(_mod), init(init) {
        v = t % _mod;
        if (v < 0)
            v += _mod;
    }

    Mint operator^ (ll k) {
        Mint res(1, _mod), tmp(v, _mod);
        while (k) {
            if (k & 1)
                res *= tmp;
            tmp *= tmp;
            k >>= 1;
        }
        return res;
    }
    static Mint add_identity(ll mod = MOD) { return Mint(0, mod); }
    static Mint mul_identity(ll mod = MOD) { return Mint(1, mod); }
    Mint inv() { return v ^ (_mod - 2); }

    Mint& operator+=(Mint a) {
        v += a.v;
        if (v >= _mod)
            v -= _mod;
        return *this;
    }
    Mint& operator-=(Mint a) {
        v += _mod - a.v;
        if (v >= _mod)
            v -= _mod;
        return *this;
    }
    Mint& operator*=(Mint a) {
        v = v * a.v % _mod;
        return *this;
    }
    Mint& operator/=(Mint a) { return (*this) *= a.inv(); }

    Mint operator+(Mint a) const { return Mint(v, _mod) += a; };
    Mint operator-(Mint a) const { return Mint(v, _mod) -= a; };
    Mint operator*(Mint a) const { return Mint(v, _mod) *= a; };
    Mint operator/(Mint a) const { return Mint(v, _mod) /= a; };

    Mint operator-() const { return v ? Mint(_mod - v, _mod) : Mint(v, _mod); }

    bool operator==(const Mint a) const { return v == a.v; }
    bool operator!=(const Mint a) const { return v != a.v; }
    bool operator<(const Mint a) const { return v < a.v; }
};
ostream& operator<<(ostream& os, Mint m) { return os << m.v; }

template <typename T>
class Matrix {
    int n, m;
    vector<vector<T>> mat;
public:
    Matrix(int n) : n(n), m(n) { mat.resize(n, vector<T>(m, T())); }
    Matrix(int n, int m) : n(n), m(m) { mat.resize(n, vector<T>(m, T())); }
    Matrix(int n, int m, T v) : n(n), m(m) { mat.resize(n, vector<T>(m, v)); }
    const vector<T>& operator[](int r) const { return mat[r]; }
    vector<T>& operator[](int r) { return mat[r]; }
    inline int N() { return n; }
    inline int M() { return m; }
    T trace() const {
        T r = T();
        for (int i = 0; i < n; i++)
            r = r + mat[i][i];
        return r;
    }
    Matrix rotate() const {
        Matrix R(m, n, T());
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                R[j][n - 1 - i] = mat[i][j];
        return R;
    }
    Matrix reflect() const {
        Matrix R(n, m);
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                R[i][m - 1 - j] = mat[i][j];
        return R;
    }
    static Matrix identity(int n) {
        Matrix R(n, n, T());
        for (int i = 0; i < R.N(); i++)
            R[i][i] = 1;
        return R;
    }
    friend Matrix operator+ (const Matrix& A, const Matrix& B) {
        Matrix R(A.N(), B.M());
        for (int i = 0; i < R.N(); i++)
            for (int j = 0; j < R.M(); j++)
                R[i][j] = A[i][j] + B[i][j], R[i][j] %= MOD;
        return R;
    }
    friend Matrix operator* (const Matrix& A, const Matrix& B) {
        Matrix R(A.n, B.m);
        for (int i = 0; i < R.n; i++)
            for (int j = 0; j < R.m; j++) {
                R[i][j] = T();
                for (int k = 0; k < R.n; k++)
                    R[i][j] = (R[i][j] + A[i][k] * B[k][j]) % MOD;
            }
        return R;
    }
    friend Matrix operator^ (const Matrix& A, long long p) {
        if (!p) return Matrix::identity(A.n);
        if (p & 1) return A * (A ^ (p - 1));
        return (A * A) ^ (p >> 1);
    }
    friend ostream& operator<<(ostream& cout, const Matrix& A) {
        for (int i = 0; i < A.n; i++) {
            for (int j = 0; j < A.m; j++)
                cout << A.mat[i][j] << ' ';
            cout << '\n';
        }
        return cout;
    }
};


int main() {

    ll N, K;
    cin >> N >> K;

    Matrix<ll> A(N);

    REP(i, N) REP(j, N) {
        cin >> A[i][j];
    }

    auto B = A ^ K;

    Mint ans = 0;
    
    REP(i, N) REP(j, N) {
        ans += B[i][j];
    }

    cout << ans << endl;

#ifdef _MSC_VER
    system("PAUSE");
#endif

    return 0;
}

Measured memory_bytes: 1536