// Original C++ solution (slower):
#include<bits/stdc++.h>

#define lli long long

using namespace std;



//Matrix Exponentiation

lli mod=1e9+7;

struct matrix //1 base index change if needed

{

    lli ara[55][55];

    int n,m;

    matrix(int n_,int m_)

    {

        n=n_;

        m=m_;

        memset(ara,0,sizeof ara);

    }

    matrix operator *(matrix a) // for multiplication

    {

        matrix ret(n,a.m);

        int nn=n;

        int mm=a.m;

        for(int i=1; i<=nn; i++)

           for(int j=1; j<=mm; j++)

               for(int k=1; k<=a.n; k++)

                {

                    ret.ara[i][j]+=(ara[i][k]*a.ara[k][j])%mod;

                    if(ret.ara[i][j]>=mod)

                    ret.ara[i][j]=(ret.ara[i][j])%mod;

                }

        return ret;

    }

    void show()  // for printing the matrix not necessary

    {

     for(int i=1; i<=n; i++,cout <<endl)

            for(int j=1; j<=m; j++)

                cout <<ara[i][j]<<" ";

    }

};

matrix identity(int n) // identity matrix

{

    matrix ret(n,n);

    for(int i=1; i<=n; i++)

        ret.ara[i][i]=1;

    return ret;

}

matrix big_modm(matrix x,lli n) //iterative bigmod

{

  matrix ret=identity(x.n);

  matrix temp=x;

  while(n)

   {

      if(n&1)

      ret=(ret*temp);

      temp=(temp*temp);

      n=n>>1;

   }

  return ret;

}



int main()

{



   long long n,k;

   cin>>n>>k;



   matrix mat(n,n);

   for(int i=1;i<=n;i++)

   {

     for(int j=1;j<=n;j++)

     {

     cin>>mat.ara[i][j];

     }

   }



   mat=big_modm(mat,k);

   long long ans=0;

    for(int i=1;i<=n;i++)

   {

     for(int j=1;j<=n;j++)

     {

     ans+=mat.ara[i][j];

     if(ans>=mod)

     ans%=mod;

     }

   }





   cout <<ans;



}




// Optimized solution to the same problem (faster):
//#pragma GCC target("avx2")

//#pragma GCC optimize("O3")

   

//#include <x86intrin.h>

#include <bits/stdc++.h>   

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

 

using namespace __gnu_pbds;

using namespace std;



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

     

#define F first

#define S second           

#define lb lower_bound               

#define ub upper_bound

#define pb push_back

#define pf push_front    

#define ppb pop_back

#define mp make_pair                                     

#define bpp __builtin_popcountll                                                                                        

#define sqr(x) ((x) * (x)) 

#define al 0x3F3F3F3F

#define sz(x) (int)x.size()

#define all(x) x.begin(), x.end()

#define in insert

#define ppf pop_front

#define endl '\n'

//#define int long long

 

typedef unsigned long long ull;

typedef long long ll;

typedef long double ld;

typedef pair <int, int> pii;

 

const int mod = (int)1e9 + 7;

const int N = (int)3e5 + 123;

const ll inf = (ll)1e18 + 1;



const double pi = acos (-1.0);

const double eps = 1e-7;                  

const int dx[] = {0, 0, 1, 0, -1};

const int dy[] = {0, 1, 0, -1, 0};



int n;

ll k;



inline int add (int a, int b) {

   a += b;

   if (a >= mod) a -= mod;

   return a;

}



inline int mult (int a, int b) {

   return 1ll * a * b % mod;

}



struct Matrix {

   int a[55][55];

   Matrix () {

      memset (a, 0, sizeof a);

   }

   void one () {

      for (int i = 1; i <= n; i ++)

         a[i][i] = 1;

   }

}a;



Matrix operator * (const Matrix &a, const Matrix &b) {

   Matrix c;

   for (int i = 1; i <= n; i ++)

      for (int j = 1; j <= n; j ++)

         for (int k = 1; k <= n; k ++)

            c.a[i][j] = add (c.a[i][j], mult (a.a[i][k], b.a[k][j]));

   return c;

}



inline Matrix binpow (Matrix a, ll b) {

   Matrix res;

   res.one ();

   while (b) {

      if (b & 1) res = res * a;

      a = a * a;

      b >>= 1;

   }

   return res;

}



inline void boost () {                    

   ios_base :: sync_with_stdio (NULL);

   cin.tie (NULL), cout.tie (NULL);          

}                                                     



inline void Solve () {

   cin >> n >> k;

   for (int i = 1; i <= n; i ++)

      for (int j = 1; j <= n; j ++)

         cin >> a.a[i][j];

   a = binpow (a, k);

   int res = 0;

   for (int i = 1; i <= n; i ++)

      for (int j = 1; j <= n; j ++)

         res = add (res, a.a[i][j]);

   cout << res;                        

}



main () {                                       

// freopen (".in", "r", stdin);    

// freopen (".out", "w", stdout);              

   boost ();

   int tt = 1;

   //cin >> tt;   

   while (tt --) {

      Solve ();

   }                                               

   return 0;      

}

                          

// Fastest known solution for this problem: