Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}

// #define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif
typedef long long ll;
typedef double ld;
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define mp(x, y) make_pair((x), (y))
#define F first
#define S second
#define I insert
#define vi vector<int>
#define vll vector<ll>
#define vpll vector<pll>
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll)(x).size()

const ll Mod = 1e9 + 7;

int dp[21][1 << 21];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int A[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> A[i][j];
  }

  for (int i = 0; i < n; i++)
    if (A[0][i] == 1)
      dp[0][1 << i] = 1;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < (1 << n); j++) {
      for (int k = 0; k < n; k++) {
        if (!(j & (1 << k)) && A[i][k] == 1) {
          dp[i][j ^ (1 << k)] += dp[i - 1][j];
          if (dp[i][j ^ (1 << k)] >= Mod)
            dp[i][j ^ (1 << k)] -= Mod;
        }
      }
    }
  }
  cout << dp[n - 1][(1 << n) - 1] << endl;
  return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}

// #define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif
typedef long long ll;
typedef double ld;
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define mp(x, y) make_pair((x), (y))
#define F first
#define S second
#define I insert
#define vi vector<int>
#define vll vector<ll>
#define vpll vector<pll>
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll)(x).size()

const ll Mod = 1e9 + 7;

int dp[21][1 << 21];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int A[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> A[i][j];
  }

  for (int i = 0; i < n; i++)
    if (A[0][i] == 1)
      dp[0][1 << i] = 1;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < (1 << n); j++) {
      if (__builtin_popcount(j) != i)
        continue;
      for (int k = 0; k < n; k++) {
        if (!(j & (1 << k)) && A[i][k] == 1) {
          dp[i][j ^ (1 << k)] += dp[i - 1][j];
          if (dp[i][j ^ (1 << k)] >= Mod)
            dp[i][j ^ (1 << k)] -= Mod;
        }
      }
    }
  }
  cout << dp[n - 1][(1 << n) - 1] << endl;
  return 0;
}