Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
#define F first
#define S second
#define migmig                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define ld long double
#define pii pair<int, int>
#define bug(x) cout << "passed " << x << endl;
#define ashar(x, y, z)                                                         \
  cout << setprecision(x) << fixed << y;                                       \
  if (z == 1)                                                                  \
    cout << "\n";
long long const inf = 1e18, linf = 2e9, mod = 1e9 + 7, inf2 = 1e12;
int const mxn = 5e6 + 10;

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}
int l[30][30];
long long dp[30][(1 << 21) + 10];
int main() {
  migmig int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> l[i][j];
  }

  for (int mask = 0; mask < (1 << n); mask++) {
    for (int i = 0; i < n; i++)
      if ((1 << i) & mask and l[0][i])
        dp[0][mask]++;
  }

  for (int v = 1; v < n; v++) {
    for (int mask = 0; mask < (1 << n); mask++) {
      if (__builtin_popcount(mask) < v)
        continue;
      for (int u = 0; u < n; u++) {
        if (l[v][u] and mask & (1 << u)) {
          dp[v][mask] += dp[v - 1][mask - (1 << u)];
          if (dp[v][mask] >= inf)
            dp[v][mask] %= mod;
        }
      }
    }
  }
  cout << dp[n - 1][(1 << n) - 1] % mod;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
#define F first
#define S second
#define migmig                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define ld long double
#define pii pair<int, int>
#define bug(x) cout << "passed " << x << endl;
#define ashar(x, y, z)                                                         \
  cout << setprecision(x) << fixed << y;                                       \
  if (z == 1)                                                                  \
    cout << "\n";
long long const inf = 1e18, linf = 2e9, mod = 1e9 + 7, inf2 = 1e12;
int const mxn = 5e6 + 10;

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}
int l[30][30];
long long dp[30][(1 << 21) + 10];
int main() {
  migmig int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> l[i][j];
  }

  for (int mask = 0; mask < (1 << n); mask++) {
    for (int i = 0; i < n; i++)
      if ((1 << i) & mask and l[0][i])
        dp[0][mask]++;
  }

  for (int v = 1; v < n; v++) {
    for (int mask = (1 << (v - 1)) - 1; mask < (1 << n); mask++) {
      if (__builtin_popcount(mask) < v)
        continue;
      for (int u = 0; u < n; u++) {
        if (l[v][u] and mask & (1 << u)) {
          dp[v][mask] += dp[v - 1][mask - (1 << u)];
          if (dp[v][mask] >= inf)
            dp[v][mask] %= mod;
        }
      }
    }
  }
  cout << dp[n - 1][(1 << n) - 1] % mod;
}