Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

#define F first
#define S second
#define ll long long
#define pb push_back
#define db double
#define ld long double
#define ppb pop_back
#define pii pair<int, int>
#define pll pair<long long, long long>
#define piii pair<int, pii>
#define all(x) (x).begin(), (x).end()
#define __                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define watch(x) cout << (#x) << " is " << (x) << endl
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define TEST                                                                   \
  int T;                                                                       \
  cin >> T;                                                                    \
  while (T--)
#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
// #define int long long

const int N = 1e5 + 1, inf = 2e9 + 7, mod = 1e9 + 7;
const ll INF = 2e18 + 7, MOD = 1e15 + 7;

int n, all;
int dp[1 << 21];
bool v[21][21], was[1 << 21];

bool has(int mask, int i) { return (mask >> i) & 1; }

int rec(int mask) {
  if (was[mask])
    return dp[mask];
  int ans = 0, bits = __builtin_popcount(mask);
  for (int i = 0; i < n; i++) {
    if (has(mask, i) && v[bits - 1][i]) {
      ans = (ans * 1LL + rec(mask ^ (1 << i))) % mod;
    }
  }
  return dp[mask] = ans;
}

signed main() {
  __
          //    freopen("lcm.in", "r", stdin);
          //    freopen("lcm.out", "w", stdout);
          cin >>
      n;
  all = (1 << n) - 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> v[i][j];
    }
  }
  dp[0] = 1;
  was[0] = 1;
  cout << rec(all);
  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

#define F first
#define S second
#define ll long long
#define pb push_back
#define db double
#define ld long double
#define ppb pop_back
#define pii pair<int, int>
#define pll pair<long long, long long>
#define piii pair<int, pii>
#define all(x) (x).begin(), (x).end()
#define __                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define watch(x) cout << (#x) << " is " << (x) << endl
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define TEST                                                                   \
  int T;                                                                       \
  cin >> T;                                                                    \
  while (T--)
#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
// #define int long long

const int N = 1e5 + 1, inf = 2e9 + 7, mod = 1e9 + 7;
const ll INF = 2e18 + 7, MOD = 1e15 + 7;

int n, all;
int dp[1 << 21];
bool v[21][21], was[1 << 21];

bool has(int mask, int i) { return (mask >> i) & 1; }

int rec(int mask) {
  if (was[mask])
    return dp[mask];
  int ans = 0, bits = __builtin_popcount(mask);
  for (int i = 0; i < n; i++) {
    if (has(mask, i) && v[bits - 1][i]) {
      ans = (ans * 1LL + rec(mask ^ (1 << i))) % mod;
    }
  }
  was[mask] = 1;
  return dp[mask] = ans;
}

signed main() {
  __
          //    freopen("lcm.in", "r", stdin);
          //    freopen("lcm.out", "w", stdout);
          cin >>
      n;
  all = (1 << n) - 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> v[i][j];
    }
  }
  dp[0] = 1;
  was[0] = 1;
  cout << rec(all);
  return 0;
}