Problem p03177 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.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>;

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
typedef long long int ll;
const ll N = 1e6;
// const ll m = 1e9 + 7;
// const ll inf= 1e14;
const ll mod = 1e9 + 7;
const ll INF = 1e14;
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define pld pair<ld, ld>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ld long double
void mul(std::vector<vector<ll>> &a, std::vector<vector<ll>> b) {
  ll n = a.size();
  std::vector<std::vector<ll>> c;
  c = a;
  // for(int i = 0;i<n;i++){
  //     for(int j = 0;j<n;j++){
  //         cout<<b[i][j]<<" ";
  //     }
  //     cout<<'\n';
  // }
  // cout<<'\n';

  // for(int i = 0;i<n;i++){
  //     for(int j = 0;j<n;j++){
  //         cout<<a[i][j]<<" ";
  //     }
  //     cout<<'\n';
  // }
  // cout<<'\n';
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      a[i][j] = 0;
      for (int k = 0; k < n; k++) {
        a[i][j] += c[i][k] * b[k][j];
        a[i][j] %= mod;
      }
    }
  }

  // for(int i = 0;i<n;i++){
  //     for(int j = 0;j<n;j++){
  //         cout<<a[i][j]<<" ";
  //     }
  //     cout<<'\n';
  // }
  // cout<<'\n';
}
void matrix_exp(std::vector<vector<ll>> &mat, ll k) {
  std::vector<std::vector<ll>> te = mat;
  while (k) {
    // cout<<k<<'\n';
    if (k % 2) {
      mul(mat, te);
    }
    k /= 2;
    mul(te, te);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  ll t = 1;
  // cin>>t;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    std::vector<vector<ll>> adj(n, vector<ll>(n));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> adj[i][j];
      }
    }
    matrix_exp(adj, k - 1);
    ll ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        ans += adj[i][j];
        ans %= mod;
      }
    }
    cout << ans << '\n';
  }
}

Fixed submission:
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.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>;

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
typedef long long int ll;
const ll N = 1e6;
// const ll m = 1e9 + 7;
// const ll inf= 1e14;
const ll mod = 1e9 + 7;
const ll INF = 1e14;
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define pld pair<ld, ld>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ld long double
void mul(std::vector<vector<ll>> &a, std::vector<vector<ll>> b) {
  ll n = a.size();
  std::vector<std::vector<ll>> c;
  c = a;
  // for(int i = 0;i<n;i++){
  //     for(int j = 0;j<n;j++){
  //         cout<<b[i][j]<<" ";
  //     }
  //     cout<<'\n';
  // }
  // cout<<'\n';

  // for(int i = 0;i<n;i++){
  //     for(int j = 0;j<n;j++){
  //         cout<<a[i][j]<<" ";
  //     }
  //     cout<<'\n';
  // }
  // cout<<'\n';
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      a[i][j] = 0;
      for (int k = 0; k < n; k++) {
        a[i][j] += c[i][k] * b[k][j];
        a[i][j] %= mod;
      }
    }
  }

  // for(int i = 0;i<n;i++){
  //     for(int j = 0;j<n;j++){
  //         cout<<a[i][j]<<" ";
  //     }
  //     cout<<'\n';
  // }
  // cout<<'\n';
}
void matrix_exp(std::vector<vector<ll>> &mat, ll k) {
  std::vector<std::vector<ll>> te = mat;
  while (k) {
    // cout<<k<<'\n';
    if (k % 2) {
      mul(mat, te);
    }
    k /= 2;
    mul(te, te);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    std::vector<vector<ll>> adj(n, vector<ll>(n));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> adj[i][j];
      }
    }
    matrix_exp(adj, k - 1);
    ll ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        ans += adj[i][j];
        ans %= mod;
      }
    }
    cout << ans << '\n';
  }
}