#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 50, MOD = 1e9 + 7;
struct mat
{
    ll a[N][N];
    mat() : a{} {}
    mat operator * (const mat & b) const
    {
        mat c;
        for (int k = 0; k < N; ++k)
            for (int i = 0; i < N; ++i)
                for (int j = 0; j < N; ++j)
                    (c.a[i][j] += a[i][k] * b.a[k][j]) %= MOD;
        return c;
    }
};
int main()
{
    int n;
    ll k;
    cin >> n >> k;
    mat a, r;
    for (int i = 0; i < n; ++i)
        for  (int j = 0; j < n; ++j) {
            cin >> a.a[i][j];
            r.a[i][j] = i == j;
        }
    for (; k; k >>= 1, a = a * a)
        if (k & 1)
            r = r * a;
    ll ans = 0;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            ans += r.a[i][j];
    cout << ans % MOD << endl;
}

