Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
const long long MOD = 1000000007;
#include <bits/stdc++.h>
using namespace std;
using ll = int_fast64_t;
using ld = long double;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
const ll INF = 1LL << 60;
void solve();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  solve();
}
#define SELECTOR(_1, _2, _3, _4, SELECT, ...) SELECT
#define rep(...) SELECTOR(__VA_ARGS__, _rep2, _rep1, _rep0)(__VA_ARGS__)
#define _rep0(i, n) for (ll i = 0; i < n; ++i)
#define _rep1(i, k, n) for (ll i = k; i < n; ++i)
#define _rep2(i, k, n, d)                                                      \
  for (ll i = k; d != 0 && d > 0 ? i < n : i > n; i += d)
template <class T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template <class... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> void contout(const T &v) {
  for (auto it = v.begin(); it != v.end();
       ++it, cout << (it != v.end() ? " " : "\n"))
    cout << *it;
}

void solve() {
  ll n;
  cin >> n;
  auto A = make_v(n, n, (ll)0);
  rep(i, n) rep(j, n) cin >> A[i][j];
  ll N = 1 << n;
  vector<ll> dp(N, 0);
  dp[N - 1] = 1;
  rep(i, n) {
    vector<ll> ndp(N, 0);
    rep(mask, N) rep(j, n) {
      if (A[i][j] && ((mask >> j) & 1)) {
        ndp[mask ^ (1 << j)] += dp[mask];
        ndp[mask ^ (1 << j)] %= MOD;
      }
    }
    dp = ndp;
  }
  cout << dp[0] << "\n";
}

Fixed submission:
const long long MOD = 1000000007;
#include <bits/stdc++.h>
using namespace std;
using ll = int_fast64_t;
using ld = long double;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
const ll INF = 1LL << 60;
void solve();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  solve();
}
#define SELECTOR(_1, _2, _3, _4, SELECT, ...) SELECT
#define rep(...) SELECTOR(__VA_ARGS__, _rep2, _rep1, _rep0)(__VA_ARGS__)
#define _rep0(i, n) for (ll i = 0; i < n; ++i)
#define _rep1(i, k, n) for (ll i = k; i < n; ++i)
#define _rep2(i, k, n, d)                                                      \
  for (ll i = k; d != 0 && d > 0 ? i < n : i > n; i += d)
template <class T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template <class... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> void contout(const T &v) {
  for (auto it = v.begin(); it != v.end();
       ++it, cout << (it != v.end() ? " " : "\n"))
    cout << *it;
}

void solve() {
  ll n;
  cin >> n;
  auto A = make_v(n, n, (ll)0);
  rep(i, n) rep(j, n) cin >> A[i][j];
  ll N = 1 << n;
  vector<ll> dp(N, 0);
  dp[N - 1] = 1;
  rep(i, n) {
    vector<ll> ndp(N, 0);
    rep(mask, N) {
      if (__builtin_popcount(mask) != (n - i))
        continue;
      rep(j, n) {
        if (A[i][j] && ((mask >> j) & 1)) {
          ndp[mask ^ (1 << j)] += dp[mask];
          ndp[mask ^ (1 << j)] %= MOD;
        }
      }
    }
    dp = ndp;
  }
  cout << dp[0] << "\n";
}