import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

enum mod = 10L ^^ 9 + 7;

void main() {
    int n; long k;
    scan(n, k);
    auto a = iota(n).map!(i => readln.split.to!(long[])).array;
    auto e = iota(n).map!(i => iota(n).map!(j => i == j ? 1L : 0L).array).array;

    debug {
        writefln("%(%s\n%)", e);
    }

    long[][] mul(long[][] a, long[][] b) {
        auto c = new long[][](n, n);
        foreach (i ; 0 .. n) {
            foreach (j ; 0 .. n) {
                foreach (k ; 0 .. n) {
                    c[i][j] += a[i][k] * b[k][j] % mod;
                    c[i][j] %= mod;
                }
            }
        }
        return c;
    }

    long[][] sqm(long[][] a) {
        return mul(a, a);
    }

    long[][] powm(long k) {
        return k > 0 ? mul(sqm(powm(k>>1)), k & 1 ? a : e) : e;
    }

    auto t = powm(k);

    long ans;

    foreach (i ; 0 .. n) {
        foreach (j ; 0 .. n) {
            ans += t[i][j];
            ans %= mod;
        }
    }

    writeln(ans);
}







void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}