Problem p03177 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;

const ll MOD = 1e9 + 7;

class Matrix {
public:
  int m, n;
  vector<vector<ll>> a;

  Matrix(int s, int t) {
    m = s;
    n = t;
    a = vector<vector<ll>>(m, vector<ll>(n, 0));
  }

  void print() {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        cout << a[i][j] << " ";
      }
      cout << endl;
    }
  }
};

Matrix mulMatrix(Matrix m1, Matrix m2) {
  int m = m1.m;
  int n = m2.n;
  int len = m1.n;

  Matrix ans = Matrix(m, n);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < len; k++) {
        ans.a[i][j] += m1.a[i][k] * m2.a[k][j];
        ans.a[i][j] %= MOD;
      }
    }
  }

  return ans;
}

Matrix mulMatrix_pow(Matrix M, int N) {
  if (N == 1)
    return M;
  else {
    if (N % 2 == 0) {
      Matrix tmp = mulMatrix_pow(M, N / 2);
      return mulMatrix(tmp, tmp);
    } else {
      Matrix tmp = mulMatrix_pow(M, (N - 1) / 2);
      return mulMatrix(M, mulMatrix(tmp, tmp));
    }
  }
}

//==================================================
int main() {
  int N;
  ll K;
  cin >> N >> K;

  Matrix A = Matrix(N, N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> A.a[j][i];
    }
  }

  // ANS = A^k * B
  Matrix B = Matrix(N, 1);
  for (int i = 0; i < N; i++)
    B.a[i][0] = 1;
  Matrix ANS = mulMatrix(mulMatrix_pow(A, K), B);

  ll ans = 0;
  for (int i = 0; i < N; i++)
    ans += ANS.a[i][0];

  cout << ans % MOD << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;

const ll MOD = 1e9 + 7;

class Matrix {
public:
  int m, n;
  vector<vector<ll>> a;

  Matrix(int s, int t) {
    m = s;
    n = t;
    a = vector<vector<ll>>(m, vector<ll>(n, 0));
  }

  void print() {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        cout << a[i][j] << " ";
      }
      cout << endl;
    }
  }
};

Matrix mulMatrix(Matrix m1, Matrix m2) {
  int m = m1.m;
  int n = m2.n;
  int len = m1.n;

  Matrix ans = Matrix(m, n);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < len; k++) {
        ans.a[i][j] += m1.a[i][k] * m2.a[k][j];
        ans.a[i][j] %= MOD;
      }
    }
  }

  return ans;
}

Matrix mulMatrix_pow(Matrix M, ll N) {
  if (N == 1)
    return M;
  else {
    if (N % 2 == 0) {
      Matrix tmp = mulMatrix_pow(M, N / 2);
      return mulMatrix(tmp, tmp);
    } else {
      Matrix tmp = mulMatrix_pow(M, (N - 1) / 2);
      return mulMatrix(M, mulMatrix(tmp, tmp));
    }
  }
}

//==================================================
int main() {
  int N;
  ll K;
  cin >> N >> K;

  Matrix A = Matrix(N, N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> A.a[j][i];
    }
  }

  // ANS = A^k * B
  Matrix B = Matrix(N, 1);
  for (int i = 0; i < N; i++)
    B.a[i][0] = 1;
  Matrix ANS = mulMatrix(mulMatrix_pow(A, K), B);

  ll ans = 0;
  for (int i = 0; i < N; i++)
    ans += ANS.a[i][0];

  cout << ans % MOD << endl;
  return 0;
}