Problem p03177 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define input                                                                  \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
ll mat[55][55];
ll temp[55][55];
ll n;
auto addmod(ll a, ll b) {
  ll res = (a % mod + b % mod) % mod;
  if (res < 0)
    res += mod;
  return res % mod;
}
auto mulmod(ll a, ll b) {
  ll res = (a % mod * b % mod) % mod;
  if (res < 0)
    res += mod;
  return res % mod;
}
void multiply(ll A[55][55], ll B[55][55]) {
  ll c[55][55];
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < n; j++) {
      c[i][j] = 0;
      for (ll k = 0; k < n; k++) {
        c[i][j] = addmod(c[i][j], mulmod(A[i][k], B[k][j]));
      }
    }
  }
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < n; j++)
      A[i][j] = c[i][j];
  }
}

int main() {
  fast;
  input;
  ll k;
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {

      cin >> mat[i][j];
      // mat[i][j];
      if (i == j)
        temp[i][j] = 1;
      else
        temp[i][j] = 0;
    }
  }

  while (k) {
    if (k & 1) {
      multiply(temp, mat);
    }
    k >>= 1;
    multiply(mat, mat);
  }
  ll res = 0;

  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < n; j++) {
      res = addmod(res, temp[i][j]);
    }
  }
  cout << res % mod << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define input                                                                  \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
ll mat[55][55];
ll temp[55][55];
ll n;
auto addmod(ll a, ll b) {
  ll res = (a % mod + b % mod) % mod;
  if (res < 0)
    res += mod;
  return res % mod;
}
auto mulmod(ll a, ll b) {
  ll res = (a % mod * b % mod) % mod;
  if (res < 0)
    res += mod;
  return res % mod;
}
void multiply(ll A[55][55], ll B[55][55]) {
  ll c[55][55];
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < n; j++) {
      c[i][j] = 0;
      for (ll k = 0; k < n; k++) {
        c[i][j] = addmod(c[i][j], mulmod(A[i][k], B[k][j]));
      }
    }
  }
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < n; j++)
      A[i][j] = c[i][j];
  }
}

int main() {
  fast;
  // input;
  ll k;
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {

      cin >> mat[i][j];
      // mat[i][j];
      if (i == j)
        temp[i][j] = 1;
      else
        temp[i][j] = 0;
    }
  }

  while (k) {
    if (k & 1) {
      multiply(temp, mat);
    }
    k >>= 1;
    multiply(mat, mat);
  }
  ll res = 0;

  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < n; j++) {
      res = addmod(res, temp[i][j]);
    }
  }
  cout << res % mod << endl;
}