#include <iostream>
#include <vector>
#include <fstream>

// directions

using namespace std;

#define bit(x, i) (x & (1 << i))          //select the bit of position i of x
#define lowbit(x) ((x) & ((x) ^ ((x)-1))) //get the lowest bit of x
#define hBit(msb, n) asm("bsrl %1,%0" \
                         : "=r"(msb)  \
                         : "r"(n)) //get the highest bit of x, maybe the fastest
#define max(a, b) (a < b ? b : a)
#define min(a, b) (a > b ? b : a)
#define abs(x) (x < 0 ? (-x) : x)    // big bug here if "-x" is not surrounded by "()"
#define IN(i, l, r) (l < i && i < r) //the next for are for checking bound
#define LINR(i, l, r) (l <= i && i <= r)
#define LIN(i, l, r) (l <= i && i < r)
#define INR(i, l, r) (l < i && i <= r)

#define wez2(n, m) \
    int(n), (m);   \
    scanf("%d %d", &(n), &(m))
#define wez3(n, m, k) \
    int(n), (m), (k); \
    scanf("%d %d %d", &(n), &(m), &(k))
#define TESTS wez(testow) while (testow--) //for multilple cases problems
#define whileZ \
    int T;     \
    getI(T);   \
    while (T--)                           // the same as above
#define getS(x) scanf("%s", x)            //get a char* string
#define clr(a, x) memset(a, x, sizeof(a)) //set elements of array to some value
#define char2Int(c) (c - '0')
#define lastEle(vec) vec[vec.size() - 1]
#define SZ(x) ((int)((x).size()))
#define REMAX(a, b) (a) = max((a), (b)) // set a to the maximum of a and b
#define REMIN(a, b) (a) = min((a), (b));
#define FOREACH(i, t) for (typeof(t.begin()) i = t.begin(); i != t.end(); i++) // traverse an STL data structure
#define ALL(c) (c).begin(), (c).end()                                          //handy for function like "sort()"
#define PRESENT(c, x) ((c).find(x) != (c).end())
#define CPRESENT(c, x) (find(ALL(c), x) != (c).end())
#define ll long long //data types used often, but you don't want to type them time by time
#define ull unsigned long long
#define us unsigned short
#define IOS ios_base::sync_with_stdio(0); //to synchronize the input of cin and scanf
#define INF 1001001001
#define PI 3.1415926535897932384626
//for map, pair
#define mp make_pair
#define fi first
#define se second

#define DBG(vari) cerr << #vari << " = " << (vari) << endl;
#define printA(a, L, R) \
    FE(i, L, R)         \
    cout << a[i] << (i == R ? '\n' : ' ')
#define printV(a) printA(a, 0, a.size() - 1)

#define ui unsigned int
//for vectors
#define pb push_back
typedef int elem_t;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<pair<int, int>> vpi;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<vpi> vvpi;
typedef pair<int, int> ii;
#define F(i, L, R) for (int i = L; i < R; i++) //next four are for "for loops"
#define FE(i, L, R) for (int i = L; i <= R; i++)
#define FF(i, L, R) for (int i = L; i > R; i--)
#define FFE(i, L, R) for (int i = L; i >= R; i--)
#define getI(a) scanf("%d", &a) //next three are handy ways to get ints, it's also force you to use '&' sign
#define getII(a, b) scanf("%d%d", &a, &b)
#define getIII(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define wez(n) \
    int(n);    \
    scanf("%d", &(n)) //handy if the input is right after the definition of a variable/

namespace EU
{

void prows(vector<ll> row)
{
    for (ll col : row)
    {
        printf("%5lld", col);
    }
    printf("\n");
}

} // namespace EU

#define maxN 100000
const int mod = 1e9 + 7;
// class Stones {
// public:  

void max_self(ll &a, ll b)
{
    a = max(a, b);
}
void add_self(int &a, int b)
{
    a = a + b;
    if (a >= mod)
        a -= mod;
}

int n;

int mul(int a, int b) {
    return (ll) a * b % mod;
}

struct M {
    vector<vector<int>> t;
    M() {
        t.resize(n, vector<int>(n));
    }
    M operator* (const M& b) const {
        M c = M();
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < n; ++j) {
                for(int k = 0; k < n; ++k) {
                    add_self(c.t[i][k], mul(t[i][j], b.t[j][k]));
                }
            }
        }
        return c;
    }
};

int main()
{
    ll k;
    cin >> n >> k;

    vvi can(n, vi(n));

    F(i, 0, n)
    {
        F(j, 0, n)
        {
            cin >> can[i][j];
        }
    }
    M answer = M();
    F(i,  0, n) answer.t[i][i] =  1;
    M m = M();
    m.t =  can;
    while(k) {
        if (k %  2) {
            answer = answer *m;
        }
        m = m * m;
        k /= 2;
    }

    // vi dp(n, 1);
    // //  dp[i] the number of wauys we got to vertex i so far;
    // for (ll step; step < k; ++step)
    // {
    //     vi new_dp(n);
    //     F(a, 0, n)
    //     {
    //         F(b, 0, n)
    //         {
    //             if (can[a][b])
    //             {
    //                 add_self(new_dp[b], dp[a]);
    //             }
    //         }
    //     }
    //     dp = new_dp;
    // }
    // int answer = 0;
    // for (int i = 0; i < n; i++)
    // {
    //     add_self(answer, dp[i]);
    // }
    int total = 0;
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            add_self(total, answer.t[i][j]);
        }
    }
    printf("%d\n", total);
    return 0;}



Measured memory_bytes: 256