#include <iostream>
#include <math.h>
#include <iomanip>
#include <bits/stdc++.h>
#include <string.h>
#include <string>
#include <algorithm>
#define ll long long int
#define pb push_back
#define fi first
#define se second
#define mem(a,b) memset(a,b,sizeof(a))
#define debug(x) cout << (#x) << " = " << x << endl;
#define debug2(i,j) cout << (#i) << "=" << i << " , " << (#j) << "=" << j << endl;

using namespace std;

ll modInverse(ll a,ll m)
{
    ll m0 = m;
    ll y = 0, x = 1;

    if (m == 1)
      return 0;

    while (a > 1)
    {
        ll q = a / m;
        ll t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0)
       x += m0;
    return x;
}

ll binomialCoeff(ll n,ll k)
{
    ll res = 1;
    if(k >n - k)
        k = n - k;
    for(int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}

ll power(ll x,ll y)
{
    ll temp;
    if(y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

/* nCr % mod
ll fact[6*N];
ll inv[6*N],invfac[6*N];
ll mod = 998244353;

void factorial()
{
    fact[0] = invfac[0] = fact[1] = invfac[1] = 1;
    inv[1] = 1;
    for(int i=2;i<=5*N + 10;i++)
    {
        fact[i] = (fact[i-1]*i)%mod;
        inv[i] = (inv[mod%i]*(mod - mod/i))%mod;
        invfac[i] = (invfac[i-1]*inv[i])%mod;
    }
}*/

bool prime[100005];

void Sieve(int n)
{
    //bool prime[n+1];
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }
    /*for (int p=2; p<=n; p++)
       if (prime[p])
          cout << p << " ";*/
}

//******************************************************    CHECK CONSTRAINTS   ***************************************************************//

const int Nmax = 1e6 + 5;
ll mod = 1e9 + 7;

int n;

void print(vector<vector<ll> > v)
{
    for(auto x : v)
    {
        for(auto y : x)
            cout << y << " ";
        cout << endl;
    }
}

vector <vector<ll> > matrix_multiply(vector<vector<ll> > a,vector<vector<ll> > b)
{
    vector < vector<ll> > mul (n,vector<ll> (n,0));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<n;k++)
            {
                mul[i][j] += (a[i][k]*b[k][j])%mod;
                mul[i][j] %= mod;
            }
        }
    }
    return mul;
}

//vector <vector<ll>> ways(n,vector<ll> (n,0));
//vector <vector<ll>> identity(n,vector<ll> (n,0));

vector <vector<ll> > power (ll k,vector <vector<ll>> identity,vector <vector<ll>> ways)
{
    vector<vector<ll> > v (n,vector<ll> (n));
    if(k==0)
        return identity;
    ll t = k/2;
    v = power(t,identity,ways);
    v = matrix_multiply(v,v);
    if(k%2)
    {
        v = matrix_multiply (ways,v);
    }
    //debug(k);
    //print(v);
    return v;
}

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    ll k;
    cin >> n >> k;
    vector<vector<ll> > ways(n,vector<ll> (n));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
            cin >> ways[i][j];
    }
    vector <vector<ll>> identity(n,vector<ll> (n,0));
    for(int i=0;i<n;i++)
        identity[i][i] = 1;
    vector <vector<ll> > v = power(k,identity,ways);
    ll ans = 0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            ans += v[i][j];
            ans %= mod;
        }
    }
    cout << ans;
}



Measured memory_bytes: 4224