Problem p01237 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;

typedef vector<int> vec;
typedef vector<vec> mat;

mat multi(const mat &A, const mat &B, int mod) {
  mat C(A.size(), vec(B[0].size()));
  for (int i = 0; i < A.size(); i++) {
    for (int j = 0; j < B.size(); j++) {
      for (int k = 0; k < B[0].size(); k++) {
        (C[i][k] += A[i][j] * B[j][k]) %= mod;
      }
    }
  }
  return C;
}

mat power(mat A, int n, int mod) {
  mat B(A.size(), vec(A.size()));
  for (int i = 0; i < A.size(); i++)
    B[i][i] = 1;
  while (n > 0) {
    if (n & 1)
      B = multi(B, A, mod);
    A = multi(A, A, mod);
    n >>= 1;
  }
  return B;
}

int dy[] = {-1, -1, 0, 0, 1, 1};
int dx[] = {-1, 0, -1, 1, 0, 1};
// int xx[40], yy[40];
// int mas[40][40];
int xx[100], yy[100];
int mas[40][40];

signed main() {
  int N, M, L;
  int tt = 0;
  while (cin >> N >> M >> L, N || M || L) {
    tt++;
    int num = 0;
    int s = 0, t = N - 1;
    // vector<pair<int, int> > pos;
    // map<pair<int, int>, int> id;
    memset(xx, -1, sizeof(xx));
    memset(yy, -1, sizeof(yy));
    memset(mas, -1, sizeof(mas));
    for (int i = 0; i < N + N - 1; i++) {
      if (i < N)
        t++;
      else
        s++;
      for (int j = s; j < t; j++) {
        // cout << num << " " << xx[num] << endl;
        // assert(xx[num] == -1);
        yy[num] = i, xx[num] = j;
        // cout << xx[num] << " " << j << endl;
        mas[i][j] = num;
        // pos.push_back(make_pair(i, j));
        // id[make_pair(i, j)] = num;
        num++;
      }
    }
    mat X(num, vec(1, 0));
    for (int i = 0; i < num; i++) {
      cin >> X[i][0];
    }

    mat A(num, vec(num, 0));
    for (int i = 0; i < num; i++) {
      int x = xx[i], y = yy[i];
      // int x, y; tie(x, y) = pos[i];
      // cout << x << " " << xx[i] << endl;
      // assert(x == xx[i] && y == yy[i]);
      for (int j = 0; j < 6; j++) {
        int nx = x + dx[j], ny = y + dy[j];
        if (0 <= nx && nx < N + N - 1 && 0 <= ny && ny < N + N - 1 &&
            mas[ny][nx] != -1) {
          // assert(id.count(make_pair(ny, nx)));
          A[i][mas[ny][nx]] = 1;
        }
      }
      A[i][i] = 1;
    }

    auto res = power(A, L, M);
    auto ans = multi(res, X, M);
    int out = 0;
    for (int i = 0; i < ans.size(); i++)
      out += ans[i][0];
    cout << "Case " << tt << ": " << out << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;

typedef vector<int> vec;
typedef vector<vec> mat;

mat multi(const mat &A, const mat &B, int mod) {
  mat C(A.size(), vec(B[0].size()));
  for (int i = 0; i < A.size(); i++) {
    for (int j = 0; j < B.size(); j++) {
      for (int k = 0; k < B[0].size(); k++) {
        (C[i][k] += A[i][j] * B[j][k]) %= mod;
      }
    }
  }
  return C;
}

mat power(mat A, int n, int mod) {
  mat B(A.size(), vec(A.size()));
  for (int i = 0; i < A.size(); i++)
    B[i][i] = 1;
  while (n > 0) {
    if (n & 1)
      B = multi(B, A, mod);
    A = multi(A, A, mod);
    n >>= 1;
  }
  return B;
}

signed main() {
  int N, M, L;
  int tt = 0;
  while (cin >> N >> M >> L, N || M || L) {
    tt++;
    int num = 0;
    int s = 0, t = N - 1;
    int xx[40], yy[40];
    int mas[40][40];
    mat X(6 * N + 1, vec(1, 0));
    memset(mas, -1, sizeof(mas));
    for (int i = 0; i < N + N - 1; i++) {
      if (i < N)
        t++;
      else
        s++;
      for (int j = s; j < t; j++) {
        cin >> X[num][0];
        yy[num] = i, xx[num] = j;
        mas[i][j] = num;
        num++;
      }
    }
    continue;
    int dy[] = {-1, -1, 0, 0, 1, 1};
    int dx[] = {-1, 0, -1, 1, 0, 1};
    mat A(num, vec(num, 0));
    for (int i = 0; i < num; i++) {
      int x = xx[i], y = yy[i];
      for (int j = 0; j < 6; j++) {
        int nx = x + dx[j], ny = y + dy[j];
        if (0 <= nx && nx < N + N - 1 && 0 <= ny && ny < N + N - 1 &&
            mas[ny][nx] != -1) {
          A[i][mas[ny][nx]] = 1;
        }
        A[i][i] = 1;
      }
    }
    /*
    for(int i = 0; i < A.size(); i++) {
      for(int j = 0; j < A[i].size(); j++) cout << A[i][j] << " " ;
      cout << endl;
    }
    */
    auto res = power(A, L, M);
    auto ans = multi(res, X, M);
    int out = 0;
    /*
    for(int i = 0; i < X.size(); i++) {
      for(int j = 0; j < X[i].size(); j++) cout << X[i][j] << " ";
      cout << endl;
    }
    */
    for (int i = 0; i < ans.size(); i++)
      out += ans[i][0];
    cout << "Case " << tt << ": " << out << endl;
  }
  return 0;
}