Problem p03177 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,sse2,sse3,sse4,mmx,tune=native")
#include <cstring>
#include <iostream>
const int MAX_N = 50;
using ull = unsigned long long;
const int MOD = 1e9 + 7;
int n;

struct Matrix {
  int a[MAX_N][MAX_N];
  Matrix() : a() {}
  inline Matrix &operator*=(const Matrix b) {
    register int i, j, k, l;
    register const int *p2;
    static ull tmp[MAX_N];
    ull cur;
    for (i = 0; i < MAX_N; ++i) {
      memset(tmp, 0, sizeof(ull) * MAX_N);
      for (k = 0, l = (MAX_N & ~15); k < l;) {
#define opt                                                                    \
  do {                                                                         \
    if (a[i][k]) {                                                             \
      for (cur = a[i][k], p2 = b.a[k], j = 0; j < MAX_N; j++, p2++)            \
        tmp[j] += cur * (*p2);                                                 \
    }                                                                          \
    k++;                                                                       \
  } while (0)
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;

        for (j = 0; j < MAX_N; j++)
          tmp[j] %= MOD;
      }

      for (; k < MAX_N;) {
        opt;
      }
#undef opt

      for (j = 0; j < MAX_N; j++)
        a[i][j] = tmp[j] % MOD;
    }
    return *this;
  }
};

inline int add(int x, int v) { return x + v >= MOD ? x + v - MOD : x + v; }

inline Matrix power(Matrix &a, long long b) {
  Matrix ret;
  for (int i = 0; i < n; ++i)
    ret.a[i][i] = 1;
  for (; b; b >>= 1, a *= a)
    if (b & 1)
      ret *= a;
  return ret;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  Matrix a;
  long long k;
  std::cin >> n >> k;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      std::cin >> a.a[i][j];
  auto ret = power(a, k);
  int ans = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      ans = add(ans, ret.a[i][j]);
  std::cout << ans;
  return 0;
}

Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,sse2,sse3,sse4,mmx,tune=native")
#include <cstring>
#include <iostream>
const int MAX_N = 50;
using ull = unsigned long long;
const int MOD = 1e9 + 7;
int n;

struct Matrix {
  int a[MAX_N][MAX_N];
  Matrix() : a() {}
  inline Matrix &operator*=(const Matrix b) {
    register int i, j, k, l;
    register const int *p2;
    static ull tmp[MAX_N];
    ull cur;
    for (i = 0; i < MAX_N; ++i) {
      memset(tmp, 0, sizeof(ull) * MAX_N);
      for (k = 0, l = (MAX_N & ~15); k < l;) {
#define opt                                                                    \
  do {                                                                         \
    if (a[i][k]) {                                                             \
      for (cur = a[i][k], p2 = b.a[k], j = 0; j < MAX_N; j++, p2++)            \
        tmp[j] += cur * (*p2);                                                 \
    }                                                                          \
    k++;                                                                       \
  } while (0)
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;
        opt;

        for (j = 0; j < MAX_N; j++)
          tmp[j] %= MOD;
      }

      for (; k < MAX_N;) {
        opt;
      }
#undef opt

      for (j = 0; j < MAX_N; j++)
        a[i][j] = tmp[j] % MOD;
    }
    return *this;
  }
};

inline int add(int x, int v) { return x + v >= MOD ? x + v - MOD : x + v; }

inline Matrix power(Matrix &a, long long b) {
  Matrix ret;
  for (int i = 0; i < n; ++i)
    ret.a[i][i] = 1;
  for (; b; b >>= 1, a *= a)
    if (b & 1)
      ret *= a;
  return ret;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  Matrix a;
  long long k;
  std::cin >> n >> k;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      std::cin >> a.a[i][j];
  auto ret = power(a, k);
  int ans = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      ans = add(ans, ret.a[i][j]);
  std::cout << ans;
  return 0;
}