Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
const int mod = 1000000007;
int dp[3000000][22];
bool field[22][22];
vector<int> in[3000000];
int n;
int main() {
  // cout.precision(10);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int in;
      cin >> in;
      if (in == 1)
        field[i][j] = true;
      else
        field[i][j] = false;
    }
  }
  for (ll bits = 0; bits < ((ll)1 << n); ++bits) {
    for (int woman = 1; woman <= n; woman++) {
      if (!(bits & ((ll)1 << (woman - 1))))
        in[bits].push_back(woman);
      // cout << bits << " " << woman << endl;
    }
  }
  dp[0][0]++;
  for (int man = 1; man <= n; man++) {
    for (ll bits = 0; bits < ((ll)1 << n); ++bits) {
      dp[bits][man] = (dp[bits][man] + dp[bits][man - 1]) % mod;
      // dp[bits][man] %= mod;
      // cout << man << " " << bits << " " << dp[bits][man - 1] << endl;
      for (int i = 0; i < in[bits].size(); i++) {
        // cout << i << endl;
        // for(ll woman = 1; woman <= n; woman++){
        int woman = in[bits][i];
        // cout << man << " " << woman << " " << bits << endl;
        // if(bits & ((ll)1<<(woman - 1))) continue;
        if (!field[man][woman])
          continue;
        // cout << man << " " << woman << " " << bits << endl;
        // dp[bits | ((ll)1<<(woman - 1))][man] += dp[bits][man - 1];
        dp[bits | ((ll)1 << (woman - 1))][man] =
            (dp[bits][man - 1] + dp[bits | ((ll)1 << (woman - 1))][man]) % mod;
      }
    }
  }
  cout << dp[((ll)1 << n) - 1][n] << endl;
  return 0;
}


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

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
const int mod = 1000000007;
int dp[3000000][22];
bool field[22][22];
vector<int> in[3000000];
int n;
int main() {
  // cout.precision(10);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int in;
      cin >> in;
      if (in == 1)
        field[i][j] = true;
      else
        field[i][j] = false;
    }
  }
  for (ll bits = 0; bits < ((ll)1 << n); ++bits) {
    for (int woman = 1; woman <= n; woman++) {
      if (!(bits & ((ll)1 << (woman - 1))))
        in[bits].push_back(woman);
      // cout << bits << " " << woman << endl;
    }
  }
  dp[0][0]++;
  for (int man = 1; man <= n; man++) {
    for (ll bits = 0; bits < ((ll)1 << n); ++bits) {
      if (__builtin_popcount(bits) != (man - 1))
        continue;
      dp[bits][man] = (dp[bits][man] + dp[bits][man - 1]) % mod;
      // dp[bits][man] %= mod;
      // cout << man << " " << bits << " " << dp[bits][man - 1] << endl;
      for (int i = 0; i < in[bits].size(); i++) {
        // cout << i << endl;
        // for(ll woman = 1; woman <= n; woman++){
        int woman = in[bits][i];
        // cout << man << " " << woman << " " << bits << endl;
        // if(bits & ((ll)1<<(woman - 1))) continue;
        if (!field[man][woman])
          continue;
        // cout << man << " " << woman << " " << bits << endl;
        // dp[bits | ((ll)1<<(woman - 1))][man] += dp[bits][man - 1];
        dp[bits | ((ll)1 << (woman - 1))][man] =
            (dp[bits][man - 1] + dp[bits | ((ll)1 << (woman - 1))][man]) % mod;
      }
    }
  }
  cout << dp[((ll)1 << n) - 1][n] << endl;
  return 0;
}