Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
// {{{
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define FOR(i, a, b)                                                           \
  for (ll i = static_cast<ll>(a); i < static_cast<ll>(b); i++)
#define FORR(i, a, b)                                                          \
  for (ll i = static_cast<ll>(a); i >= static_cast<ll>(b); i--)
#define REP(i, n) for (ll i = 0ll; i < static_cast<ll>(n); i++)
#define REPR(i, n) for (ll i = static_cast<ll>(n); i >= 0ll; i--)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> IP;
typedef pair<ll, LP> LLP;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};

constexpr int INF = 100000000;
constexpr ll LINF = 10000000000000000ll;
constexpr int MOD = static_cast<int>(1e9 + 7);
constexpr double EPS = 1e-9;

static inline ll mod(ll x, ll m) {
  ll y = x % m;
  return (y >= 0 ? y : y + m);
}

// }}}

int N;
int a[21][21];
int dp[25][1 << 21];

void solve() {
  dp[0][0] = 1;
  FOR(i, 1, N + 1) {
    REP(j, (1 << N) - 1) {
      REP(k, N) {
        if ((j & (1 << k)) == 0 && a[i - 1][k]) {
          dp[i][j | (1 << k)] += dp[i - 1][j];
          dp[i][j | (1 << k)] %= MOD;
        }
      }
    }
  }
  cout << dp[N][(1 << N) - 1] << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  REP(i, N) REP(j, N) cin >> a[i][j];
  solve();
  return 0;
}

// vim:set foldmethod=marker:


Fixed submission:
// {{{
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define FOR(i, a, b)                                                           \
  for (ll i = static_cast<ll>(a); i < static_cast<ll>(b); i++)
#define FORR(i, a, b)                                                          \
  for (ll i = static_cast<ll>(a); i >= static_cast<ll>(b); i--)
#define REP(i, n) for (ll i = 0ll; i < static_cast<ll>(n); i++)
#define REPR(i, n) for (ll i = static_cast<ll>(n); i >= 0ll; i--)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> IP;
typedef pair<ll, LP> LLP;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};

constexpr int INF = 100000000;
constexpr ll LINF = 10000000000000000ll;
constexpr int MOD = static_cast<int>(1e9 + 7);
constexpr double EPS = 1e-9;

static inline ll mod(ll x, ll m) {
  ll y = x % m;
  return (y >= 0 ? y : y + m);
}

// }}}

int N;
int a[21][21];
int dp[25][1 << 21];

void solve() {
  dp[0][0] = 1;
  FOR(i, 1, N + 1) {
    REP(j, (1 << N) - 1) {
      if (dp[i - 1][j] == 0)
        continue;
      REP(k, N) {
        if ((j & (1 << k)) == 0 && a[i - 1][k]) {
          dp[i][j | (1 << k)] += dp[i - 1][j];
          dp[i][j | (1 << k)] %= MOD;
        }
      }
    }
  }
  cout << dp[N][(1 << N) - 1] << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  REP(i, N) REP(j, N) cin >> a[i][j];
  solve();
  return 0;
}

// vim:set foldmethod=marker: