Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define L "\n"
#define S " "
#define ll long long
#define pb push_back
#define mp make_pair
#define vi std::vector<int>
#define si std::set<int>
#define vl std::vector<long long>
#define qi std::queue<int>
#define s1(a) scanf("%d", &a)
#define s2(a, b) scanf("%d %d", &a, &b)
#define s3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define de(a) cout << #a << "  ------>  " << a << "\n"
#define SIZE 23
#define MOD 1000000007
#define INF INT_MAX
#define rep(i, n) for (i = 1; i <= n; i++)
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

using namespace std;
int a[SIZE][SIZE];

ll dp[23][4100000];
bool flag[23][2100000];

vi w;
int n;

ll f(int men, int num) {
  if (men == n + 1)
    return 1;
  int now = 0, j;
  rep(j, n) {
    if (a[men][j] == 0 || w[j] == 0)
      continue;
    w[j] = 0;
    num |= 1 << (j - 1);
    if (flag[men + 1][num])
      now = (now + dp[men + 1][num]) % MOD;
    else
      now = (now + f(men + 1, num)) % MOD;
    w[j] = 1;
    num &= ~(1 << (j - 1));
  }
  flag[men][num] = 1;
  return dp[men][num] = now;
}

void solve() {
  int i, j;
  cin >> n;
  w.assign(n + 1, 1);
  rep(i, n) {
    rep(j, n) { cin >> a[i][j]; }
  }
  cout << f(1, 0) << L;
}
int main() {
  FASTIO;
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  int t;
  // cin >> t;
  // while (t--)
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define L "\n"
#define S " "
#define ll long long
#define pb push_back
#define mp make_pair
#define vi std::vector<int>
#define si std::set<int>
#define vl std::vector<long long>
#define qi std::queue<int>
#define s1(a) scanf("%d", &a)
#define s2(a, b) scanf("%d %d", &a, &b)
#define s3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define de(a) cout << #a << "  ------>  " << a << "\n"
#define SIZE 23
#define MOD 1000000007
#define INF INT_MAX
#define rep(i, n) for (i = 1; i <= n; i++)
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

using namespace std;
int a[SIZE][SIZE];

ll dp[23][4100000];
bool flag[23][2100000];

vi w;
int n;

ll f(int men, int num) {
  if (men == n + 1)
    return 1;
  int now = 0, j;
  rep(j, n) {
    if (a[men][j] == 0 || w[j] == 0)
      continue;
    w[j] = 0;
    num |= 1 << (j - 1);
    if (flag[men + 1][num])
      now = (now + dp[men + 1][num]) % MOD;
    else
      now = (now + f(men + 1, num)) % MOD;
    w[j] = 1;
    num &= ~(1 << (j - 1));
  }
  flag[men][num] = 1;
  return dp[men][num] = now;
}

void solve() {
  int i, j;
  cin >> n;
  w.assign(n + 1, 1);
  rep(i, n) {
    rep(j, n) { cin >> a[i][j]; }
  }
  cout << f(1, 0) << L;
}
int main() {
  FASTIO;
  // #ifndef ONLINE_JUDGE
  //     freopen("in.txt", "r", stdin);
  // #endif
  int t;
  // cin >> t;
  // while (t--)
  solve();
  return 0;
}