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

#include <iostream>
#include <vector>
using namespace std;

const long long p = 1000000007;

void printmat(const vector<int> &a) {
  const int n = a.size();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cerr << a[(i + n - j) % n] << " ";
    }
    cerr << endl;
  }
}

// caution: b = a * b
void mulBy(const vector<int> &a, vector<int> &b, int n) {
  vector<int> r(n);
  for (int i = 0; i < n; i++) {
    r[i] = b[i];
  }
  for (int j = 0; j < n; j++) {
    long long c = 0;
    for (int k = 0; k < n; k++) {
      c += (a[k] * (long long)r[(j + n - k) % n]) %
           p; // b'[i][j] += a[i][k] * b[k][j];
    }
    b[j] = (int)(c % p);
  }
}
// b = b * b
void pow2(vector<int> &b, int n) {
  vector<int> r(n);
  for (int i = 0; i < n; i++) {
    r[i] = b[i];
  }
  mulBy(r, b, n);
}
// mat = mat^x
void pow(vector<int> &mat, int n, int x) {
  vector<int> r(n);
  // eye
  for (int j = 0; j < n; j++) {
    r[j] = 0 == j ? 1 : 0;
  }
  while (x > 0) {
    if ((x & 1) != 0) {
      mulBy(mat, r, n);
    }
    pow2(mat, n);
    x >>= 1;
  }
  for (int j = 0; j < n; j++) {
    mat[j] = r[j];
  }
}

int main(int argc, char *argv[]) {
  for (;;) {
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> ps;
    for (int i = 0; i < m; i++) {
      int pi;
      cin >> pi;
      ps.push_back(pi);
    }

    // hist[i] .. possibilities of being on i
    // hist' = hist * mat
    //   where mat[i][j] is transition possibilities from i to j

    vector<int> mat(n);
    for (int j = 0; j < n; j++) {
      mat[j] = 0;
    }
    // kachi
    for (int j = 0; j < m; j++) {
      mat[(int)((ps[j]) % n)]++;
    }
    mat[0]++; // make
    // printmat(mat);
    pow(mat, n, k);
    for (int i = 0; i < n; i++) {
      cout << mat[i] << endl;
    }
    break;
  }
  return 0;
}

Error: 0
1 0 0 0 2 0 0 1 0 0 
0 1 0 0 0 2 0 0 1 0 
0 0 1 0 0 0 2 0 0 1 
1 0 0 1 0 0 0 2 0 0 
0 1 0 0 1 0 0 0 2 0 
0 0 1 0 0 1 0 0 0 2 
2 0 0 1 0 0 1 0 0 0 
0 2 0 0 1 0 0 1 0 0 
0 0 2 0 0 1 0 0 1 0 
0 0 0 2 0 0 1 0 0 1 


Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

const long long p = 1000000007;

void printmat(const vector<int> &a) {
  const int n = a.size();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cerr << a[(i + n - j) % n] << " ";
    }
    cerr << endl;
  }
}

// caution: b = a * b
void mulBy(const vector<int> &a, vector<int> &b, int n) {
  vector<int> r(n);
  for (int i = 0; i < n; i++) {
    r[i] = b[i];
  }
  for (int j = 0; j < n; j++) {
    long long c = 0;
    for (int k = 0; k < n; k++) {
      c += (a[k] * (long long)r[(j + n - k) % n]) %
           p; // b'[i][j] += a[i][k] * b[k][j];
    }
    b[j] = (int)(c % p);
  }
}
// b = b * b
void pow2(vector<int> &b, int n) {
  vector<int> r(n);
  for (int i = 0; i < n; i++) {
    r[i] = b[i];
  }
  mulBy(r, b, n);
}
// mat = mat^x
void pow(vector<int> &mat, int n, int x) {
  vector<int> r(n);
  // eye
  for (int j = 0; j < n; j++) {
    r[j] = 0 == j ? 1 : 0;
  }
  while (x > 0) {
    if ((x & 1) != 0) {
      mulBy(mat, r, n);
    }
    pow2(mat, n);
    x >>= 1;
  }
  for (int j = 0; j < n; j++) {
    mat[j] = r[j];
  }
}

int main(int argc, char *argv[]) {
  for (;;) {
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> ps;
    for (int i = 0; i < m; i++) {
      int pi;
      cin >> pi;
      ps.push_back(pi);
    }

    // hist[i] .. possibilities of being on i
    // hist' = hist * mat
    //   where mat[i][j] is transition possibilities from i to j

    vector<int> mat(n);
    for (int j = 0; j < n; j++) {
      mat[j] = 0;
    }
    // kachi
    for (int j = 0; j < m; j++) {
      mat[(int)((ps[j]) % n)]++;
    }
    mat[0]++; // make
    printmat(mat);
    pow(mat, n, k);
    for (int i = 0; i < n; i++) {
      cout << mat[i] << endl;
    }
    break;
  }
  return 0;
}

/*
void printmat(const vector< vector<int> > &a)
{
  const int n = a.size();
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
      cerr << a[i][j] << " ";
    }
    cerr << endl;
  }
}

// caution: b = a * b
void mulBy(const vector< vector<int> > &a, vector< vector<int> > &b, int n)
{
  vector< vector<int> > r(n, vector<int>(n));
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
      r[i][j] = b[j][i];
    }
  }
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
      long long c = 0;
      for(int k = 0; k < n; k++) {
        c += (a[i][k] * (long long)r[j][k]) % p; // b'[i][j] += a[i][k] *
b[k][j];
      }
    b[i][j] = (int)(c % p);
    }
  }
}
// b = b * b
void pow2(vector< vector<int> > &b, int n)
{
  vector< vector<int> > r(n, vector<int>(n));
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
      r[i][j] = b[i][j];
    }
  }
  mulBy(r, b, n);
}
// mat = mat^x
void pow(vector< vector<int> > &mat, int n, int x)
{
  vector< vector<int> > r(n, vector<int>(n));
  // eye
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
      r[i][j] = i == j ? 1 : 0;
    }
  }
  while(x > 0) {
    if((x & 1) != 0) {
      mulBy(mat, r, n);
    }
    pow2(mat, n);
    x >>= 1;
  }
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
      mat[i][j] = r[i][j];
    }
  }
}

int main(int argc, char *argv[])
{
  for(;;) {
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> ps;
    for(int i = 0; i < m; i++) {
      int pi;
      cin >> pi;
      ps.push_back(pi);
    }

    // hist[i] .. possibilities of being on i
    // hist' = hist * mat
    //   where mat[i][j] is transition possibilities from i to j

    vector< vector<int> > mat(n, vector<int>(n, 0));
    for(int i = 0; i < n; i++) {
      for(int j = 0; j < n; j++) {
        mat[i][j] = 0;
      }
      // kachi
      for(int j = 0; j < m; j++) {
        mat[i][(int)((ps[j] + i) % n)]++;
      }
      mat[i][i]++; // make
    }
    //printmat(mat);
    pow(mat, n, k);
    for(int i = 0; i < n; i++) {
      cout << mat[0][i] << endl;
    }
    break;
  }
  return 0;
}
*/