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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int a[10];
bool used[11];

int judge() {
  /*  int A = (a[0]+a[1]+a[2])%j, B = (a[3]+a[4]+a[5])%j, C =
  (a[6]+a[7]+a[8])%j; int D = (a[0]+a[3]+a[6])%j, E = (a[1]+a[4]+a[7])%j, F =
  (a[2]+a[5]+a[8])%j; int G = (a[0]+a[4]+a[8])%j, H = (a[2]+a[4]+a[6])%j; if(A
  != B) return 0; if(A != B || B != C || C != D || D != E || E != F || F != G ||
  G != H || H != A) return 0;*/

  /*  int A = (a[0]+a[1]+a[2]), B = (a[3]+a[4]+a[5]), C = (a[6]+a[7]+a[8]);
  int D = (a[0]+a[3]+a[6]), E = (a[1]+a[4]+a[7]), F = (a[2]+a[5]+a[8]);
  int G = (a[0]+a[4]+a[8]), H = (a[2]+a[4]+a[6]);
  A %= j; B %= j; C %= j; D %= j; E %= j; F %= j; G %= j; H %= j;
  if((int)A != (int)B) return 0;*/
  //  if(A != B || B != C || C != D || D != E || E != F || F != G || G != H || H
  //  != A) return 0;
  /*  rep(i, 3){
    rep(j, 3) printf("%d ", a[i*3+j]);
    puts("");
    }*/
  int mod = a[9];
  int sum = (a[0] + a[1] + a[2]) % mod;
  if ((a[3] + a[4] + a[5]) % mod != sum || (a[6] + a[7] + a[8]) % mod != sum)
    return 0;
  if ((a[0] + a[3] + a[6]) % mod != sum || (a[1] + a[4] + a[7]) % mod != sum ||
      (a[2] + a[5] + a[8]) % mod != sum)
    return 0;
  if ((a[0] + a[4] + a[8]) % mod != sum || (a[2] + a[4] + a[6]) % mod != sum)
    return 0;
  return 1;
}

int dfs(int n, bool *used) {
  if (n == 10)
    return judge();
  if (a[n])
    return dfs(n + 1, used);
  int res = 0;
  REP(i, 1, 11) {
    if (used[i])
      continue;
    a[n] = i;
    used[i] = 1;
    res += dfs(n + 1, used);
    a[n] = 0;
    used[i] = 0;
  }
  return res;
}

int main() {
  while (1) {
    bool used[11];
    memset(used, 0, sizeof(used));
    rep(i, 10) {
      scanf("%d", a + i);
      if (a[i])
        used[a[i]] = 1;
    }
    if (a[0] == -1)
      break;

    printf("%d\\n", dfs(0, used));
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int a[10];
bool used[11];

int judge() {
  /*  int A = (a[0]+a[1]+a[2])%j, B = (a[3]+a[4]+a[5])%j, C =
  (a[6]+a[7]+a[8])%j; int D = (a[0]+a[3]+a[6])%j, E = (a[1]+a[4]+a[7])%j, F =
  (a[2]+a[5]+a[8])%j; int G = (a[0]+a[4]+a[8])%j, H = (a[2]+a[4]+a[6])%j; if(A
  != B) return 0; if(A != B || B != C || C != D || D != E || E != F || F != G ||
  G != H || H != A) return 0;*/

  /*  int A = (a[0]+a[1]+a[2]), B = (a[3]+a[4]+a[5]), C = (a[6]+a[7]+a[8]);
  int D = (a[0]+a[3]+a[6]), E = (a[1]+a[4]+a[7]), F = (a[2]+a[5]+a[8]);
  int G = (a[0]+a[4]+a[8]), H = (a[2]+a[4]+a[6]);
  A %= j; B %= j; C %= j; D %= j; E %= j; F %= j; G %= j; H %= j;
  if((int)A != (int)B) return 0;*/
  //  if(A != B || B != C || C != D || D != E || E != F || F != G || G != H || H
  //  != A) return 0;
  /*  rep(i, 3){
    rep(j, 3) printf("%d ", a[i*3+j]);
    puts("");
    }*/
  int mod = a[9];
  int sum = (a[0] + a[1] + a[2]) % mod;
  if ((a[3] + a[4] + a[5]) % mod != sum || (a[6] + a[7] + a[8]) % mod != sum)
    return 0;
  return 1;
}

int dfs(int n) {
  if (n == 9)
    return judge();
  if (a[n])
    return dfs(n + 1);
  int res = 0;
  REP(i, 1, 11) {
    if (used[i])
      continue;
    a[n] = i;
    used[i] = 1;
    res += dfs(n + 1);
    a[n] = 0;
    used[i] = 0;
  }
  return res;
}

int main() {
  while (scanf("%d%d%d%d%d%d%d%d%d%d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5],
               &a[6], &a[7], &a[8], &a[9]) &&
         a[0] != -1) {
    fill(used, used + 11, false);
    rep(i, 10) if (a[i]) used[a[i]] = 1;
    printf("%d\\n", dfs(0));
  }
  return 0;
}