#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define SPEED ios_base::sync_with_stdio(false); cin.tie(NULL);
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define RFOR(i, b, a) for (ll i = b; i >= a; --i)
#define ALL(x) x.begin(), x.end()
#define DEBUG(args...) { string _s = #args; replace(ALL(_s), ' ', '\0');\
replace(ALL(_s), ',', ' ');\
stringstream _ss(_s); istream_iterator<string> _it(_ss); _debug(_it, args);}
#define endl "\n"
#define F first
#define S second
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void _debug(istream_iterator<string>) {}
template<typename T, typename... Args>
void _debug(istream_iterator<string> it, T first, Args... args) {
    cerr << ">> " << *it << " : " << first << endl; _debug(++it, args...);
}
template <typename T1, typename T2>
inline ostream& operator << (ostream& out, const pair<T1, T2>& p) {
    return out << "(" << p.F << ", " << p.S << ")";
}
template<typename T>
inline ostream& operator << (ostream& out, const vector<T>& v) {
    if (v.empty()) return out << "[]";
    else { out << '['; for (auto& e : v) { out << e << ", "; } return out << "\b\b]"; }
}
template<typename T>
inline ostream& operator << (ostream& out, const set<T>& s) {
    if (s.empty()) return out << "{}";
    else { out << '{'; for (auto& e : s) { out << e << ", "; } return out << "\b\b}"; }
}
template<typename T>
inline ostream& operator << (ostream& out, const unordered_set<T>& s) {
    return out << set<T>(ALL(s));
}
template<typename T1, typename T2>
inline ostream& operator << (ostream& out, const map<T1, T2>& m) {
    if (m.empty()) return out << "{}";
    out << '{'; for (auto& p : m) { out << p << ", "; } return out << "\b\b}";
}
template<typename T1, typename T2>
inline ostream& operator << (ostream& out, const unordered_map<T1, T2>& m) {
    return out << map<T1, T2>(ALL(m));
}
template<typename T>
inline ostream& operator << (ostream& out, const ordered_set<T>& s) {
    return out << set<T>(ALL(s));
}

typedef long long ll;
typedef long double ld;
typedef vector<long long> vll;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vpll;
typedef unordered_map<ll, ll> STll;

/**********************************************************************************
* MATRIX DATA TYPE WITH IN-BUILT '*', '^', '=', '<<' OPERATORS   @author: manishjoshi394
**********************************************************************************/
class Matrix
{
    ///"Writing this class was Awesome ! DATED: 10 Oct-2018, 21:57 @NIT-UK(Strike Days)"///
    vector<vector<ll>> arr; ;
public:
    static ll MOD;
    const int N;
    const int M;

    Matrix(const int _N, const int _M, ll init = 0)
    : arr(_N, vector<ll>(_M, init)), N(_N), M(_M) {}

    Matrix(const int N) : Matrix(N, N) { ; };

    vector<ll>& operator[](int i)
    {
        return arr[i];
    }

    Matrix operator*(const Matrix& that)
    {
        assert(this->M == that.N);
        Matrix product(this->N, that.M, 0);
        FOR(i, 0, this->N)
        {
            FOR(j, 0, that.M)
            {
                FOR(r, 0, this->M)
                {
                    product[i][j] += this->arr[i][r] * that.arr[r][j];
                    product[i][j] %= Matrix::MOD;
                }
            }
        }
        return product;
    }

    Matrix operator^(ll exp)
    {
        Matrix base = *(this);
        Matrix result = Matrix::identity(base.N);
        while (exp > 0)
        {
            if (exp & 1) result = result * base;
            base = (base * base);
            exp >>= 1;
        }
        return result;
    }

    Matrix& operator=(const Matrix& m) {
        assert(m.N == this->N);
        assert(m.M == this->M);
        FOR(i, 0, N)
            FOR(j, 0, M)
                arr[i][j] = m.arr[i][j];
        return *(this);
    }

    Matrix& operator=(const initializer_list<ll>& _l)
    {
        auto it = _l.begin();
        FOR(i, 0, N)
        {
            FOR(j, 0, M)
            {
                if (it != _l.end())
                    arr[i][j] = *(it++);
                else break;
            }
        }
        return *(this);
    }

    static Matrix identity(ll N)
    {
        Matrix _i(N);
        FOR(i, 0, N)
            FOR(j, 0, N)
                if (i == j) _i.arr[i][j] = 1;
        return _i;
    }
    friend ostream& operator<<(ostream&, const Matrix&);
};

inline ostream& operator << (ostream& out, const Matrix& m) {
    FOR(i, 0, m.N) {
        cout << '|';
        FOR(j, 0, m.M) {
            cout << m.arr[i][j];
            (j != m.M - 1) ? cout << "   " : cout;
        }
        cout << '|' << endl;
    }
    return out;
}
ll Matrix::MOD = 1e9 + 7;  // MOD to be used in Matrix Exponentiation & Multiplication


/************************************** MAIN PROGRAM ********************************************/

int main()
{
    //freopen("input.txt", "r", stdin);
    SPEED
    ll n, k;
    cin >> n >> k;
    Matrix A(n);
    FOR(i, 0, n)
    {
        FOR(j, 0, n)
        {
            cin >> A[i][j];
        }
    }
    //DEBUG(A)
    A = A ^ k;
    //DEBUG(A)
    ll ans = 0;
    FOR(i, 0, n)
    {
        FOR(j, 0, n)
        {
            ans += A[i][j];
            if (ans >= Matrix::MOD) {
                ans -= Matrix::MOD;
            }
        }
    }
    cout << ans;
}
/************************************** END OF PROGRAM ******************************************/


Measured memory_bytes: 384