Problem p03177 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define forn(i, n) for (ll i = 0; i < n; i++)
#define fore(i, a, b) for (ll i = a; i <= b; i++)
#define ford(i, n) for (ll i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define endl "\n"
#define all(a) a.begin(), a.end()
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define PI 3.14159265

/*************************************************************************************/
void file() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
}
/*************************************************************************************/

const ll maxn = 2e5 + 1, mod = 1e9 + 7;
ll mat[51][51], extr[51][51], n;

void mul(ll b[51][51]) {

  ll m[n][n];
  forn(i, n) {
    forn(j, n) {
      m[i][j] = 0;
      forn(k, n) {
        ll temp = ((mat[i][k] % mod) * (b[k][j] % mod)) % mod;
        m[i][j] = ((m[i][j] % mod) + (temp % mod)) % mod;
      }
    }
  }
  forn(i, n) forn(j, n) mat[i][j] = m[i][j] % mod;
}

void rec(ll k) {
  if (k <= 1)
    return;
  rec(k / 2);
  mul(mat);
  if (k % 2)
    mul(extr);
}

int main() {

  sync file();
  ll k;
  cin >> n >> k;
  forn(i, n) forn(j, n) cin >> mat[i][j], extr[i][j] = mat[i][j];
  rec(k);
  ll ans = 0;
  forn(i, n) forn(j, n) ans = (ans + mat[i][j]) % mod;
  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define forn(i, n) for (ll i = 0; i < n; i++)
#define fore(i, a, b) for (ll i = a; i <= b; i++)
#define ford(i, n) for (ll i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define endl "\n"
#define all(a) a.begin(), a.end()
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define PI 3.14159265

/*************************************************************************************/
void file() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
}
/*************************************************************************************/

const ll maxn = 2e5 + 1, mod = 1e9 + 7;
ll mat[51][51], extr[51][51], n;

void mul(ll b[51][51]) {

  ll m[n][n];
  forn(i, n) {
    forn(j, n) {
      m[i][j] = 0;
      forn(k, n) {
        ll temp = ((mat[i][k] % mod) * (b[k][j] % mod)) % mod;
        m[i][j] = ((m[i][j] % mod) + (temp % mod)) % mod;
      }
    }
  }
  forn(i, n) forn(j, n) mat[i][j] = m[i][j] % mod;
}

void rec(ll k) {
  if (k <= 1)
    return;
  rec(k / 2);
  mul(mat);
  if (k % 2)
    mul(extr);
}

int main() {

  sync
      // file();
      ll k;
  cin >> n >> k;
  forn(i, n) forn(j, n) cin >> mat[i][j], extr[i][j] = mat[i][j];
  rec(k);
  ll ans = 0;
  forn(i, n) forn(j, n) ans = (ans + mat[i][j]) % mod;
  cout << ans;

  return 0;
}