Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second

template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class T> istream &operator>>(istream &is, vector<T> &xs) {
  for (auto &x : xs)
    is >> x;
  return is;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &xs) {
  for (unsigned int i = 0; i < xs.size(); ++i)
    os << (i ? " " : "") << xs[i];
  return os;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}
void debug(istringstream &) {}
template <char sep = ',', class Head, class... Tail>
void debug(istringstream &iss, Head &&head, Tail &&...tail) {
  string name;
  getline(iss, name, ',');
  cerr << sep << name << "=" << head;
  debug(iss, forward<Tail>(tail)...);
}
#define DEBUG(...)                                                             \
  do {                                                                         \
    istringstream ss(#__VA_ARGS__);                                            \
    debug<' '>(ss, __VA_ARGS__);                                               \
    cerr << endl;                                                              \
  } while (0)

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

LL dp[21][1 << 21];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  ;
  cin >> N;

  VVI xs(N, VI(N));
  cin >> xs;
  const int B = 1 << N;

  dp[0][0] = 1;
  REP(i, N) {
    REP(b, B) {
      if (!dp[i][b])
        continue;
      REP(k, N) {
        if (!xs[i][k] || b >> k & 1)
          continue;
        int nb = b | (1 << k);
        dp[i + 1][nb] += dp[i][b];
        if (dp[i + 1][nb] >= MOD)
          dp[i + 1][nb] -= MOD;
      }
    }
  }

  cout << dp[N][B - 1] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second

template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class T> istream &operator>>(istream &is, vector<T> &xs) {
  for (auto &x : xs)
    is >> x;
  return is;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &xs) {
  for (unsigned int i = 0; i < xs.size(); ++i)
    os << (i ? " " : "") << xs[i];
  return os;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}
void debug(istringstream &) {}
template <char sep = ',', class Head, class... Tail>
void debug(istringstream &iss, Head &&head, Tail &&...tail) {
  string name;
  getline(iss, name, ',');
  cerr << sep << name << "=" << head;
  debug(iss, forward<Tail>(tail)...);
}
#define DEBUG(...)                                                             \
  do {                                                                         \
    istringstream ss(#__VA_ARGS__);                                            \
    debug<' '>(ss, __VA_ARGS__);                                               \
    cerr << endl;                                                              \
  } while (0)

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

LL dp[22][1 << 21];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  ;
  cin >> N;

  VVI xs(N, VI(N));
  cin >> xs;
  const int B = 1 << N;

  dp[0][0] = 1;
  REP(i, N) {
    REP(b, B) {
      if (!dp[i][b])
        continue;
      REP(k, N) {
        if (!xs[i][k] || b >> k & 1)
          continue;
        int nb = b | (1 << k);
        dp[i + 1][nb] += dp[i][b];
        if (dp[i + 1][nb] >= MOD)
          dp[i + 1][nb] -= MOD;
      }
    }
  }

  cout << dp[N][B - 1] << endl;

  return 0;
}