Problem p03177 (C++)

Buggy submission (status: Runtime Error):
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define f first
#define s second
#define pb push_back
#define REP(i, a, b) for (long long int i = a; i < b; i++)
#define mp make_pair
#define INF 1000000000000000000
#define mod 1000000007
#define pi pair<ll, ll>
#define pd pair<ld, ld>
#define vi vector<ll>
#define vd vector<ld>
#define vvi vector<vi>
#define vvd vector<vd>
#define vpi vector<pi>
#define vpd vector<pd>
#define us unordered_set<ll>
#define um unordered_map<ll, ll>
#define sortA(a) sort(a.begin(), a.end())
#define sortD(a) sort(a.begin(), a.end(), greater<ll>())
#define all(a) a.begin(), a.end()
#define N 100005

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
#define in insert
#define fbo find_by_order
#define ook order_of_key

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif

ll powmod(ll x, ll y) {
  if (x == 0)
    return 0;
  if (y == 0 || x == 1)
    return 1;
  if (y % 2 == 0)
    return powmod((x * x) % mod, y / 2) % mod;
  return (x * powmod(x, y - 1) % mod) % mod;
}

vvi mul(vvi A, vvi B, ll m) {
  ll n = A.size();
  vvi C(n, vi(n));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      for (ll k = 0; k < n; ++k) {
        C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % m;
      }
    }
  }
  return C;
}

// when n is integer
vvi matexp(vvi M, ll n, ll m) {
  if (n == 0) {
    for (ll i = 0; i < (ll)M.size(); ++i) {
      fill(M[i].begin(), M[i].end(), 0);
      M[i][i] = 1;
    }
    return M;
  }
  if (n == 1)
    return M;
  if (n % 2 == 0) {
    return matexp(mul(M, M, m), n / 2, m);
  } else {
    return mul(M, matexp(M, n - 1, m), m);
  }
}
void solve() {
  ll n, k;
  cin >> n >> k;
  vvi a(n, vi(n));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      cin >> a[i][j];
    }
  }
  vvi M = matexp(a, k, mod);
  ll ans = 0;
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      ans = (ans + M[i][j]) % mod;
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}


Fixed submission:
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define f first
#define s second
#define pb push_back
#define REP(i, a, b) for (long long int i = a; i < b; i++)
#define mp make_pair
#define INF 1000000000000000000
#define mod 1000000007
#define pi pair<ll, ll>
#define pd pair<ld, ld>
#define vi vector<ll>
#define vd vector<ld>
#define vvi vector<vi>
#define vvd vector<vd>
#define vpi vector<pi>
#define vpd vector<pd>
#define us unordered_set<ll>
#define um unordered_map<ll, ll>
#define sortA(a) sort(a.begin(), a.end())
#define sortD(a) sort(a.begin(), a.end(), greater<ll>())
#define all(a) a.begin(), a.end()
#define N 100005

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
#define in insert
#define fbo find_by_order
#define ook order_of_key

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif

ll powmod(ll x, ll y) {
  if (x == 0)
    return 0;
  if (y == 0 || x == 1)
    return 1;
  if (y % 2 == 0)
    return powmod((x * x) % mod, y / 2) % mod;
  return (x * powmod(x, y - 1) % mod) % mod;
}

vvi mul(vvi A, vvi B, ll m) {
  ll n = A.size();
  vvi C(n, vi(n));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      for (ll k = 0; k < n; ++k) {
        C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % m;
      }
    }
  }
  return C;
}

// when n is integer
vvi matexp(vvi M, ll n, ll m) {
  if (n == 0) {
    for (ll i = 0; i < (ll)M.size(); ++i) {
      fill(M[i].begin(), M[i].end(), 0);
      M[i][i] = 1;
    }
    return M;
  }
  if (n == 1)
    return M;
  if (n % 2 == 0) {
    return matexp(mul(M, M, m), n / 2, m);
  } else {
    return mul(M, matexp(M, n - 1, m), m);
  }
}
void solve() {
  ll n, k;
  cin >> n >> k;
  vvi a(n, vi(n));
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      cin >> a[i][j];
    }
  }
  vvi M = matexp(a, k, mod);
  ll ans = 0;
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j < n; ++j) {
      ans = (ans + M[i][j]) % mod;
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}