Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define fi first
#define se second
#define mp make_pair
#define itrfor(itr, A) for (auto itr = A.begin(); itr != A.end(); itr++)
template <class T>
using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>;
typedef long long llong;
char moji[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char moji2[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                  'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                  'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char moji3[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
#define Sort(a) sort(a.begin(), a.end());
#define Reverse(a) reverse(a.begin(), a.end());
#define print(a) cout << a << endl;
#define MOD llong(1e9 + 7)
#define MAX int(2 * 1e5 + 5)
#define debug(x) cout << #x << " = " << (x) << endl;
#define pi acos(-1.0)
#define int llong
#define INF llong(1e17)
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
void myprint(int *A, int A_num) {
  REP(i, A_num) cout << A[i] << " ";
  cout << endl;
}

const int N_max = 22;
const int n_MAX = 1024 * 1024 * 2 + 5;

signed main() {
  int N;
  cin >> N;
  int A[N_max][N_max];
  REP(i, N) REP(j, N) cin >> A[i][j];

  int n = 1;
  int joutai[25];
  REP(i, N) {
    joutai[i] = n;
    n *= 2;
  }

  int dp[2][n_MAX];
  REP(j, 2) REP(i, n_MAX) dp[j][i] = 0;
  int woman_id = 0;
  while (1) {
    if (A[0][woman_id])
      dp[1][joutai[woman_id]] = 1;
    woman_id++;
    if (woman_id == N)
      break;
  }

  FOR(man, 1, N) {
    FOR(j, 1, n) {
      REP(woman, N) {
        if (joutai[woman] & j)
          continue;
        if (A[man][woman] == 0)
          continue;
        dp[(man + 1) % 2][j + joutai[woman]] += dp[man % 2][j];
        dp[(man + 1) % 2][j + joutai[woman]] %= MOD;
      }
    }
  }
  cout << dp[N % 2][n - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define fi first
#define se second
#define mp make_pair
#define itrfor(itr, A) for (auto itr = A.begin(); itr != A.end(); itr++)
template <class T>
using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>;
typedef long long llong;
char moji[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char moji2[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                  'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                  'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char moji3[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
#define Sort(a) sort(a.begin(), a.end());
#define Reverse(a) reverse(a.begin(), a.end());
#define print(a) cout << a << endl;
#define MOD llong(1e9 + 7)
#define MAX int(2 * 1e5 + 5)
#define debug(x) cout << #x << " = " << (x) << endl;
#define pi acos(-1.0)
#define int llong
#define INF llong(1e17)
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
void myprint(int *A, int A_num) {
  REP(i, A_num) cout << A[i] << " ";
  cout << endl;
}

const int N_max = 22;
const int n_MAX = 1024 * 1024 * 2 + 5;

signed main() {
  int N;
  cin >> N;
  int A[N_max][N_max];
  REP(i, N) REP(j, N) cin >> A[i][j];

  int n = 1;
  int joutai[25];
  REP(i, N) {
    joutai[i] = n;
    n *= 2;
  }

  int dp[2][n_MAX];
  REP(j, 2) REP(i, n_MAX) dp[j][i] = 0;
  int woman_id = 0;
  while (1) {
    if (A[0][woman_id])
      dp[1][joutai[woman_id]] = 1;
    woman_id++;
    if (woman_id == N)
      break;
  }

  FOR(man, 1, N) {
    FOR(j, 1, n) {
      if (dp[man % 2][j] == 0)
        continue;
      REP(woman, N) {
        if (joutai[woman] & j)
          continue;
        if (A[man][woman] == 0)
          continue;
        dp[(man + 1) % 2][j + joutai[woman]] += dp[man % 2][j];
        dp[(man + 1) % 2][j + joutai[woman]] %= MOD;
      }
    }
  }
  cout << dp[N % 2][n - 1] << endl;
}