prithivMLmods/Coder-Stat huggingface dataset:
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define MOD 1000000007
using namespace std;

int a[22][22];
long long dp[22][2107152];

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> a[i][j];

  dp[0][0] = 1;
  int nnn = 1 << n;
  for (int i = 0; i < n; i++) {
    for (int mask = 0; mask < nnn; mask++) {
      for (int j = 0; j < n; j++) {
        if (a[i][j] && (mask & (1 << j)) == 0) {
          int newMask = mask | (1 << j);
          dp[i + 1][newMask] += dp[i][mask];
          if (dp[i + 1][newMask] >= MOD) {
            dp[i + 1][newMask] -= MOD;
          }
        }
      }
    }
  }

  cout << dp[n][(1 << n) - 1];

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define MOD 1000000007
using namespace std;

int a[22][22];
long long dp[22][2107152];

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> a[i][j];

  dp[0][0] = 1;
  int nnn = 1 << n;
  for (int i = 0; i < n; i++) {
    for (int mask = 0; mask < nnn; mask++) {
      if (__builtin_popcount(mask) != i)
        continue;
      for (int j = 0; j < n; j++) {
        if (a[i][j] && (mask & (1 << j)) == 0) {
          int newMask = mask | (1 << j);
          dp[i + 1][newMask] += dp[i][mask];
          if (dp[i + 1][newMask] >= MOD) {
            dp[i + 1][newMask] -= MOD;
          }
        }
      }
    }
  }

  cout << dp[n][(1 << n) - 1];

  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

ll dp[1000000];
ll arr[22][22];
ll n;
ll m = 1000000000 + 7;

ll solve(ll mask) {
  ll j = __builtin_popcount(mask);

  if (j == n)
    return 1;

  if (dp[mask] != -1)
    return dp[mask];

  ll ans = 0;

  for (int i = 0; i < n; i++) {
    if (arr[j][i] && !(mask & (1 << i))) {
      ans = (ans % m + solve(mask | (1 << i)) % m) % m;
    }
  }
  dp[mask] = ans;

  return dp[mask];
}

int main() {
  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> arr[i][j];
    }
  }
  cout << solve(0) << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

ll dp[2097152];
ll arr[25][25];
ll n;
ll m = 1000000000 + 7;

ll solve(ll mask) {
  ll j = __builtin_popcount(mask);

  if (j == n)
    return 1;

  if (dp[mask] != -1)
    return dp[mask];

  ll ans = 0;

  for (int i = 0; i < n; i++) {
    if (arr[j][i] && !(mask & (1 << i))) {
      ans = (ans % m + solve(mask | (1 << i)) % m) % m;
    }
  }
  dp[mask] = ans;

  return dp[mask];
}

int main() {
  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> arr[i][j];
    }
  }
  cout << solve(0) << endl;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

template <class T> inline T chmax(T &a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T> inline T chmin(T &a, const T b) {
  return a = (a > b) ? b : a;
}

constexpr int MOD = 1e9 + 7;
constexpr int inf = 1e9;
constexpr long long INF = 1e18;

#define all(a) (a).begin(), (a).end()

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> a[i][j];
  }

  vector<vector<ll>> dp(n + 1, vector<ll>((1 << n), 0));
  dp[0][0] = 1;

  for (int bit = 0; bit < (1 << n); bit++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (!(bit & (1 << j)) && a[i][j]) {
          dp[i + 1][bit | (1 << j)] += dp[i][bit];
          dp[i + 1][bit | (1 << j)] %= MOD;
        }
      }
    }
  }

  cout << dp[n][(1 << n) - 1] << endl;

  return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

template <class T> inline T chmax(T &a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T> inline T chmin(T &a, const T b) {
  return a = (a > b) ? b : a;
}

constexpr int MOD = 1e9 + 7;
constexpr int inf = 1e9;
constexpr long long INF = 1e18;

#define all(a) (a).begin(), (a).end()

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> a[i][j];
  }

  vector<vector<ll>> dp(n + 1, vector<ll>((1 << n), 0));
  dp[0][0] = 1;

  for (int bit = 0; bit < (1 << n); bit++) {
    int i = __builtin_popcount(bit);
    for (int j = 0; j < n; j++) {
      if (!(bit & (1 << j)) && a[i][j]) {
        dp[i + 1][bit | (1 << j)] += dp[i][bit];
        dp[i + 1][bit | (1 << j)] %= MOD;
      }
    }
  }

  cout << dp[n][(1 << n) - 1] << endl;

  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <vector>

using namespace std;

int upbound;
const int MOD = 1000000007;
vector<int> g, dp;

inline int Solve(int x) {
  if (x == 0)
    return 1;
  if (dp[x] != -1)
    return dp[x];
  int p = __builtin_popcount(x), r = 0;
  for (int y = x & g[p - 1]; y; y &= y - 1) {
    r += Solve(x & ~(y & -y));
    if (r >= MOD)
      r -= MOD;
  }
  return r;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, r;
  char p;
  cin >> n;
  upbound = (1 << n);
  g = vector<int>(n), dp = vector<int>(upbound, -1);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) {
      cin >> p;
      if (p == '1')
        g[i] |= (1 << j);
    }
  cout << Solve(upbound - 1);
}

Fixed submission:
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <vector>

using namespace std;

int upbound;
const int MOD = 1000000007;
vector<int> g, dp;

inline int Solve(int x) {
  if (x == 0)
    return 1;
  if (dp[x] != -1)
    return dp[x];
  int p = __builtin_popcount(x), r = 0;
  for (int y = x & g[p - 1]; y; y &= y - 1) {
    r += Solve(x & ~(y & -y));
    if (r >= MOD)
      r -= MOD;
  }
  dp[x] = r;
  return r;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, r;
  char p;
  cin >> n;
  upbound = (1 << n);
  g = vector<int>(n), dp = vector<int>(upbound, -1);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) {
      cin >> p;
      if (p == '1')
        g[i] |= (1 << j);
    }
  cout << Solve(upbound - 1);
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long

const int MAX_N = 22;
const int MOD = 1e9 + 7;

int to[MAX_N][MAX_N];
int dp[MAX_N][(1 << MAX_N)];

void modadd(int &a, int b) { a = (a + b) % MOD; }

signed main(void) {

  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> to[i][j];
    }
  }

  // 一番はじめの男についてだけ愚直に計算する

  for (int f = 0; f < N; f++) {
    if (to[0][f]) {
      dp[0][(1 << f)]++;
    }
  }

  for (int male = 0; male < N - 1; male++) { // 何番目の男までを見たか
    for (int female = 0; female < (1 << N);
         female++) { // どの女がまだ残っているか
      // 次に追加するのは male+1　番目の男である

      for (int check = 0; check < N; check++) {
        if (!(female >> check & 1) && to[male + 1][check]) {
          // まだ残っていて、かつmaleと相性がいい女がいたら合わせる
          modadd(dp[male + 1][female | (1 << check)], dp[male][female]);
        }
      }
    }
  }

  cout << dp[N - 1][(1 << N) - 1] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long

const int MAX_N = 22;
const int MOD = 1e9 + 7;

int to[MAX_N][MAX_N];
int dp[MAX_N][(1 << MAX_N)];

void modadd(int &a, int b) { a = (a + b) % MOD; }

signed main(void) {

  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> to[i][j];
    }
  }

  // 一番はじめの男についてだけ愚直に計算する

  for (int f = 0; f < N; f++) {
    if (to[0][f]) {
      dp[0][(1 << f)]++;
    }
  }

  for (int male = 0; male < N - 1; male++) { // 何番目の男までを見たか
    for (int female = 0; female < (1 << N);
         female++) { // どの女がまだ残っているか
      // 次に追加するのは male+1　番目の男である

      // 枝刈り

      int cnt = 0;
      for (int i = 0; i < N; i++) {
        if (female >> i & 1) {
          cnt++;
        }
      }

      if (cnt != male + 1) {
        continue;
      }

      for (int check = 0; check < N; check++) {
        if (!(female >> check & 1) && to[male + 1][check]) {
          // まだ残っていて、かつmaleと相性がいい女がいたら合わせる
          modadd(dp[male + 1][female | (1 << check)], dp[male][female]);
        }
      }
    }
  }

  cout << dp[N - 1][(1 << N) - 1] << endl;

  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define md 1000000007
#define mx 1e18
#define pb push_back
#define endl '\n'
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define pii pair<ll int, ll int>
#define ff first
#define ss second

int main() {
  fastio
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll int n;
  cin >> n;

  ll int ar[n + 1][n + 1];
  for (ll int i = 0; i < n; i++)
    for (ll int j = 0; j < n; j++)
      cin >> ar[i][j];

  ll int dp[(1 << n) + 1];
  memset(dp, 0, sizeof(dp));

  dp[0] = 1LL;

  // for(ll int i=0;i<n;i++)
  // {
  for (ll int j = 0; j < (1 << n); j++) {
    ll int i = __builtin_popcount(j);
    for (ll int k = 0; k < n; k++) {
      if (ar[i][k] && !(j & (1LL << k))) {
        ll int nm = j ^ (1LL << k);
        dp[nm] += dp[j];
        dp[nm] %= md;
      }
    }
  }
  // }

  cout << dp[(1 << n) - 1] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define md 1000000007
#define mx 1e18
#define pb push_back
#define endl '\n'
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define pii pair<ll int, ll int>
#define ff first
#define ss second

int main() {
  // fastio
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt" , "r" , stdin);
  // freopen("output.txt" , "w" , stdout);
  // #endif

  ll int n;
  cin >> n;

  ll int ar[n + 1][n + 1];
  for (ll int i = 0; i < n; i++)
    for (ll int j = 0; j < n; j++)
      cin >> ar[i][j];

  ll int dp[(1 << n) + 1];
  memset(dp, 0, sizeof(dp));

  dp[0] = 1LL;

  // for(ll int i=0;i<n;i++)
  // {
  for (ll int j = 0; j < (1 << n); j++) {
    ll int i = __builtin_popcount(j);
    for (ll int k = 0; k < n; k++) {
      if (ar[i][k] && !(j & (1LL << k))) {
        ll int nm = j ^ (1LL << k);
        dp[nm] += dp[j];
        dp[nm] %= md;
      }
    }
  }
  // }

  cout << dp[(1 << n) - 1] << endl;

  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

static const int mod = 1000000007;
static const int MAX_N = 21;
int DP[1 << MAX_N][MAX_N]; // メモ化テーブル
int N;
int a[MAX_N][MAX_N];
int rec(int S, int V);
int calc();

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> a[i][j];
    }
  }

  for (int i = 0; i < 1 << N; i++) {
    for (int j = 0; j < N; j++) {
      DP[i][j] = -1;
    }
  }

  cout << calc() << endl;

  return 0;
}

int rec(int S, int V, int n) {
  if (DP[S][V] >= 0) {
    return DP[S][V];
  }

  if (!a[n][V]) {
    DP[S][V] = 0;
    return 0;
  } else if (n == N - 1) {
    DP[S][V] = 1;
    return DP[S][V];
  }

  int sum = 0;
  int nextS = S | (1 << V);
  for (int i = 0; i < N; i++) {
    if (i != V && !(S >> i & 1)) {
      // なるべく関数呼び出しを少なくすると少し速くなる...
      sum += DP[nextS][i] >= 0 ? DP[nextS][i] : rec(nextS, i, n + 1);
      sum %= mod;
    }
  }

  DP[S][V] = sum;
  return DP[S][V];
}

int calc() {
  int sum = 0;
  for (int i = 0; i < N; i++) {
    sum += rec(0, i, 0);
    sum %= mod;
  }
  return sum;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

static const int mod = 1000000007;
static const int MAX_N = 21;
int DP[1 << MAX_N][MAX_N]; // メモ化テーブル
int N;
int a[MAX_N][MAX_N];
int rec(int S, int V);
int calc();

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> a[i][j];
    }
  }

  std::fill(DP[0], DP[(1 << MAX_N) - 1], -1);

  cout << calc() << endl;

  return 0;
}

int rec(int S, int V, int n) {
  if (DP[S][V] >= 0) {
    return DP[S][V];
  }

  if (!a[n][V]) {
    DP[S][V] = 0;
    return 0;
  } else if (n == N - 1) {
    DP[S][V] = 1;
    return DP[S][V];
  }

  int sum = 0;
  int nextS = S | (1 << V);
  for (int i = 0; i < N; i++) {
    if (i != V && !(S >> i & 1)) {
      // なるべく関数呼び出しを少なくすると少し速くなる...
      sum += DP[nextS][i] >= 0 ? DP[nextS][i] : rec(nextS, i, n + 1);
      sum %= mod;
    }
  }

  DP[S][V] = sum;
  return DP[S][V];
}

int calc() {
  int sum = 0;
  for (int i = 0; i < N; i++) {
    sum += rec(0, i, 0);
    sum %= mod;
  }
  return sum;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int>> piii;
#define pb push_back
#define debug(x) cout << x << endl
#define fastio ios_base::sync_with_stdio(0), cin.tie(0)
#define PI acos(-1)
#define all(c) c.begin(), c.end()
#define SET(x, y) memset((x), y, sizeof(x))
const int MOD = 1e9 + 7;
const int INF = 1e9 + 5;
const LL INF64 = 1e18;

const int N = 1e5 + 5;

bool grid[25][25];
LL dp[25][(1LL << 22)];

int main() {
  fastio;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n; j++)
      cin >> grid[i][j];
  }
  SET(dp, 0);
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (1 << n); j++) {
      for (int k = 0; k < n; k++) {
        int setted = n - k - 1;
        if (grid[i][k] == 1 && !(j & (1 << setted))) {
          dp[i][j ^ (1 << setted)] =
              (dp[i][j ^ (1 << setted)] + dp[i - 1][j]) % MOD;
        }
      }
    }
  }
  cout << dp[n][(1 << n) - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int>> piii;
#define pb push_back
#define debug(x) cout << x << endl
#define fastio ios_base::sync_with_stdio(0), cin.tie(0)
#define PI acos(-1)
#define all(c) c.begin(), c.end()
#define SET(x, y) memset((x), y, sizeof(x))
const int MOD = 1e9 + 7;
const int INF = 1e9 + 5;
const LL INF64 = 1e18;

const int N = 1e5 + 5;

bool grid[25][25];
LL dp[25][(1LL << 22)];

int main() {
  fastio;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n; j++)
      cin >> grid[i][j];
  }
  SET(dp, 0);
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (1 << n); j++) {
      if (dp[i - 1][j] == 0 || __builtin_popcount(j) != i - 1)
        continue;
      for (int k = 0; k < n; k++) {
        int setted = n - k - 1;
        if (grid[i][k] == 1 && !(j & (1 << setted))) {
          dp[i][j ^ (1 << setted)] =
              (dp[i][j ^ (1 << setted)] + dp[i - 1][j]) % MOD;
        }
      }
    }
  }
  cout << dp[n][(1 << n) - 1] << endl;
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define maxn 23
#define maxm (1 << 21) + 5
#define mod 1000000007
int dp[maxm] = {};
int main() {
  int n;
  int a[maxn][maxn];
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      scanf("%d", &a[i][j]);
  int M = (1 << n) - 1;
  dp[0] = 1;
  for (int i = 0; i < n; i++)
    for (int j = M; j >= 0; j--)
      for (int k = 0; k < n; k++)
        if (j & (1 << k) && a[i][k])
          dp[j] = (dp[j] + dp[j - (1 << k)]) % mod;
  printf("%d\n", dp[M]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define maxn 23
#define maxm (1 << 21) + 5
#define mod 1000000007
int dp[maxm] = {};
int main() {
  int n;
  int a[maxn][maxn];
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      scanf("%d", &a[i][j]);
  int M = (1 << n) - 1;
  dp[0] = 1;
  /*for(int i=0; i<n; i++)
          for(int j=M; j>=0; j--)
                  for(int k=0; k<n; k++)
                          if(j&(1<<k) && a[i][k])
                                  dp[j] = (dp[j] + dp[j-(1<<k)]) % mod;*/
  for (int j = 0; j <= M; j++) {
    int i = __builtin_popcount(j) - 1;
    for (int k = 0; k < n; k++)
      if (j & (1 << k) && a[i][k])
        dp[j] = (dp[j] + dp[j - (1 << k)]) % mod;
  }
  printf("%d\n", dp[M]);
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
// @idea:
//  dp(i, mask) : #ways of matching man i with women bitmask, mask
//                  given that [0, i-1] men already matched
//  dp(i, mask) = sum {dp(i - 1, mask.clear(j)) | j <- mask.ones, a[i][j] == 1}

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;

const int NMAX = 21 + 2;
const int MOD = 1e9 + 7;

int n, a[NMAX][NMAX];

int solve(void) {
  vector<vi> dp(1LL << n, vi(n + 1, 0));

  dp[0][0] = 1;
  for (int mask = 1; mask < (1LL << n); ++mask) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        if (((mask >> (j - 1)) & 1LL) and a[i - 1][j - 1]) {
          dp[mask][i] =
              (dp[mask][i] + dp[mask ^ (1LL << (j - 1))][i - 1]) % MOD;
        }
      }
    }
  }

  return dp[(1 << n) - 1][n];
}

int main(void) {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      cin >> a[i][j];

  cout << solve() << endl;
  return 0;
}


Fixed submission:
// @idea:
//  dp(i, mask) : #ways of matching man i with women bitmask, mask
//                  given that [0, i-1] men already matched
//  dp(i, mask) = sum {dp(i - 1, mask.clear(j)) | j <- mask.ones, a[i][j] == 1}

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;

const int NMAX = 21 + 2;
const int MOD = 1e9 + 7;

int n, a[NMAX][NMAX];

int solve(void) {
  vector<vi> dp(1LL << n, vi(n + 1, 0));

  dp[0][0] = 1;
  for (int mask = 1; mask < (1LL << n); ++mask) {
    for (int i = 1; i <= n; ++i) {
      if (__builtin_popcount(mask) != i)
        continue;
      for (int j = 1; j <= n; ++j) {
        if (((mask >> (j - 1)) & 1LL) and a[i - 1][j - 1]) {
          dp[mask][i] =
              (dp[mask][i] + dp[mask ^ (1LL << (j - 1))][i - 1]) % MOD;
        }
      }
    }
  }

  return dp[(1 << n) - 1][n];
}

int main(void) {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      cin >> a[i][j];

  cout << solve() << endl;
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

const int maxn = 22;
const int mo = 1e9 + 7;

int n, a[maxn][maxn], f[maxn][1 << maxn];

int countOne(int cur) {
  int res = 0;
  while (cur > 0) {
    if (cur & 1)
      ++res;

    cur >>= 1;
  }
  return res;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      cin >> a[i][j];

  int lim = (1 << n) - 1;

  f[0][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = (1 << i) - 1; j <= lim; ++j)
      // if (countOne(j) == i) {
      for (int k = 0; k < n; ++k)
        if (a[i - 1][k] && (j & (1 << k))) {
          f[i][j] += f[i - 1][j - (1 << k)];
          if (f[i][j] > mo)
            f[i][j] -= mo;
        }
  // }

  cout << f[n][lim] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

const int maxn = 22;
const int mo = 1e9 + 7;

int n, a[maxn][maxn], f[maxn][1 << maxn];

int countOne(int cur) {
  int res = 0;
  while (cur > 0) {
    if (cur & 1)
      ++res;

    cur >>= 1;
  }
  return res;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      cin >> a[i][j];

  int lim = (1 << n) - 1;

  f[0][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = (1 << i) - 1; j <= lim; ++j)
      if (countOne(j) == i) {
        for (int k = 0; k < n; ++k)
          if ((j & (1 << k)) && a[i - 1][k]) {
            f[i][j] += f[i - 1][j - (1 << k)];
            if (f[i][j] >= mo)
              f[i][j] -= mo;
          }
      }

  cout << f[n][lim] << endl;

  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstring>
#include <iostream>
using namespace std;

const int MAXN = 21;          // maximum number of couples
const int MAXL = 1 << 21 + 3; // maximum number of couples
const int BIG_NUM = 1000000007;

int N;              // number of couples
int dp[MAXN][MAXL]; // dp lookup table
int comp[MAXN];     // compatibility

int solve(int currI, int usedJ) {
  if (currI == N)
    return (__builtin_popcount(usedJ) == N); // if all are used

  if (dp[currI][usedJ] != -1)
    return dp[currI][usedJ];

  int total = 0;

  for (int j = 0; j < N; j++)
    if (!(usedJ & (1 << j)) && comp[currI] & (1 << j))
      total = (total + solve(currI + 1, usedJ | (1 << j))) % BIG_NUM;

  return dp[currI][usedJ] = total;
}

int main() {
  cin >> N;

  memset(dp, -1, sizeof(dp));
  memset(comp, 0, sizeof(comp));

  int temp;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      cin >> temp;
      comp[i] |= temp << j;
    }

  cout << solve(0, 0) << endl;
}


Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;

const int MAXN = 21;          // maximum number of couples
const int MAXL = 1 << 21 + 2; // maximum number of couples
const int BIG_NUM = 1000000007;

int N;              // number of couples
int dp[MAXN][MAXL]; // dp lookup table
int comp[MAXN];     // compatibility

int solve(int currI, int usedJ) {
  if (currI == N)
    return (__builtin_popcount(usedJ) == N); // if all are used

  if (dp[currI][usedJ] != -1)
    return dp[currI][usedJ];

  int total = 0;

  for (int j = 0; j < N; j++)
    if (!(usedJ & (1 << j)) && comp[currI] & (1 << j))
      total = (total + solve(currI + 1, usedJ | (1 << j))) % BIG_NUM;

  return dp[currI][usedJ] = total;
}

int main() {
  cin >> N;

  memset(dp, -1, sizeof(dp));
  memset(comp, 0, sizeof(comp));

  int temp;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      cin >> temp;
      comp[i] |= temp << j;
    }

  cout << solve(0, 0) << endl;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
// S.G.N.//
#include <bits/stdc++.h>
using namespace std;
typedef vector<long long> vi;
typedef long long ll;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define endl "\n"
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define fr(n) for (ll i = 0; i < n; i++)
#define tr(a) for (auto it = a.begin(); it != a.end(); it++)
#define N 998244353445
#define PI 3.1415926535897932384
#define F first
#define S second
#define mp make_pair
#define FAST                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mod 1000000007

void solve() {
  ll n;
  cin >> n;
  ll a[n][n];
  fr(n) {
    rep(j, 0, n) { cin >> a[i][j]; }
  }
  ll dp[2][1 << n];
  fr(2) {
    rep(j, 0, 1 << n) { dp[i][j] = 0; }
  }

  dp[0][0] = 1;
  rep(i, 1, n + 1) {
    rep(j, 0, 1 << n) {
      dp[i % 2][j] = 0;
      rep(k, 0, n) {
        if (j & 1 << k && a[i - 1][k]) {

          dp[i % 2][j] += dp[(i - 1) % 2][j ^ 1 << k];
          dp[i % 2][j] = dp[i % 2][j] % mod;
        }
      }
    }
  }

  cout << dp[n % 2][(1 << n) - 1];
}
int main() {

#ifndef ONLINE_JUDGE
  *freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  FAST ll test;
  test = 1;
  // cin>>test;
  while (test--) {
    solve();
  }
  return 0;
}


Fixed submission:
// S.G.N.//
#include <bits/stdc++.h>
using namespace std;
typedef vector<long long> vi;
typedef long long ll;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define endl "\n"
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define fr(n) for (ll i = 0; i < n; i++)
#define tr(a) for (auto it = a.begin(); it != a.end(); it++)
#define N 998244353445
#define PI 3.1415926535897932384
#define F first
#define S second
#define mp make_pair
#define FAST                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mod 1000000007

void solve() {
  ll n;
  cin >> n;
  ll a[n][n];
  fr(n) {
    rep(j, 0, n) { cin >> a[i][j]; }
  }
  ll dp[2][1 << n];
  fr(2) {
    rep(j, 0, 1 << n) { dp[i][j] = 0; }
  }

  dp[0][0] = 1;
  rep(i, 1, n + 1) {
    rep(j, 0, 1 << n) {
      dp[i % 2][j] = 0;
      if (__builtin_popcount(j) != i)
        continue;
      rep(k, 0, n) {
        if (j & 1 << k && a[i - 1][k]) {

          dp[i % 2][j] += dp[(i - 1) % 2][j ^ 1 << k];
          dp[i % 2][j] = dp[i % 2][j] % mod;
        }
      }
    }
  }

  cout << dp[n % 2][(1 << n) - 1];
}
int main() {

#ifndef ONLINE_JUDGE
  *freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  FAST ll test;
  test = 1;
  // cin>>test;
  while (test--) {
    solve();
  }
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, m, n) for (int i = m; i < (n); i++)
typedef long long ll;
typedef long double ld;
typedef vector<int> VI;
typedef vector<ll> VL;

const ll MOD = 1000000007;

int N;
int A[22][22];
ll dp[21][1 << 21];
vector<int> masks[21];

void solve() {
  cin >> N;
  REP(i, N) REP(j, N) cin >> A[i][j];
  dp[0][0] = 1;

  REP(mask, 1 << N) masks[__builtin_popcount(mask)].push_back(mask);

  REP(i, N) for (auto mask : masks[i]) {
    REP(j, N) {
      if (!A[i][j] || (mask & (1 << j)))
        continue;
      (dp[i + 1][mask | (1 << j)] += dp[i][mask]) %= MOD;
    }
  }

  cout << dp[N][(1 << N) - 1] << "\n";
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  solve();
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, m, n) for (int i = m; i < (n); i++)
typedef long long ll;
typedef long double ld;
typedef vector<int> VI;
typedef vector<ll> VL;

const ll MOD = 1000000007;

int N;
int A[25][25];
ll dp[23][1 << 21];
vector<int> masks[25];

void solve() {
  cin >> N;
  REP(i, N) REP(j, N) cin >> A[i][j];
  dp[0][0] = 1;

  REP(mask, 1 << N) masks[__builtin_popcount(mask)].push_back(mask);

  REP(i, N) for (auto mask : masks[i]) {
    REP(j, N) {
      if (!A[i][j] || (mask & (1 << j)))
        continue;
      (dp[i + 1][mask | (1 << j)] += dp[i][mask]) %= MOD;
    }
  }

  cout << dp[N][(1 << N) - 1] << "\n";
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  solve();
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
// https://atcoder.jp/contests/dp/tasks/dp_o
#include <bits/stdc++.h>
#define ALL(e) e.begin(), e.end()
#define RALL(e) e.rbegin(), e.rend()
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define dbg(x) (cerr << #x << ":" << x)
#define mid (l + r) / 2
#define fi first
#define sc second
#define N 1000000009

using namespace std;

typedef long long int lli;

void mod(lli &a) {
  if (a > 1e9 + 7)
    a -= (1e9 + 7);
}

int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;

  vector<vector<int>> ar(n + 1, vector<int>(n + 1, 0));

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> ar[i][j];

  lli mask = 1 << n;
  vector<lli> dp(mask + 5, 0);

  dp[0] = 1;

  for (int i = 0; i < mask; i++) {
    int a = __builtin_popcount(i);
    // cerr << a << endl;
    for (int j = 0; j < n; j++) {
      if (ar[a][j] and !(i & (1 << j))) {
        cerr << a << " " << j << endl;
        mod(dp[i]);
        dp[i | (1 << j)] += dp[i];
        mod(dp[i | (1 << j)]);
      }
    }
  }

  cout << dp[mask - 1] << endl;

  return 0;
}

Fixed submission:
// https://atcoder.jp/contests/dp/tasks/dp_o
#include <bits/stdc++.h>
#define ALL(e) e.begin(), e.end()
#define RALL(e) e.rbegin(), e.rend()
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define dbg(x) (cerr << #x << ":" << x)
#define mid (l + r) / 2
#define fi first
#define sc second
#define N 1000000009

using namespace std;

typedef long long int lli;

void mod(lli &a) {
  if (a > 1e9 + 7)
    a -= (1e9 + 7);
}

int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;

  vector<vector<int>> ar(n + 1, vector<int>(n + 1, 0));

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> ar[i][j];

  lli mask = 1 << n;
  vector<lli> dp(mask + 5, 0);

  dp[0] = 1;

  for (int i = 0; i < mask; i++) {
    int a = __builtin_popcount(i);
    // cerr << a << endl;
    for (int j = 0; j < n; j++) {
      if (ar[a][j] and !(i & (1 << j))) {
        mod(dp[i]);
        dp[i | (1 << j)] += dp[i];
        mod(dp[i | (1 << j)]);
      }
    }
  }

  cout << dp[mask - 1] << endl;

  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)

const int MOD = 1e+9 + 7;

int main() {
  int N;
  cin >> N;
  vector<vector<int>> a(N, vector<int>(N));
  REP(i, N) REP(j, N) cin >> a[i][j];

  // dp[i][j]: 男性は 1-index, 女性は 0 index で状態
  //           i 番目の男性までマッチングしたとき、 女性 j
  //           もマッチングが終わっている場合の数
  vector<vector<long long>> dp(N + 1, vector<long long>(1 << N));
  dp[0][0] = 1;
  REP(i, N) {
    REP(j, (1 << N)) {
      REP(k, N) {
        if (!(j & (1 << k)) and a[i][k] == 1) {
          dp[i + 1][j + (1 << k)] += dp[i][j];
          dp[i + 1][j + (1 << k)] %= MOD;
        }
      }
    }
  }
  cout << dp[N][(1 << N) - 1] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)

const int MOD = 1e+9 + 7;

int main() {
  int N;
  cin >> N;
  vector<vector<int>> a(N, vector<int>(N));
  REP(i, N) REP(j, N) cin >> a[i][j];

  // dp[i][j]: 男性は 1-index, 女性は 0 index で状態
  //           i 番目の男性までマッチングしたとき、 女性 j
  //           もマッチングが終わっている場合の数
  vector<vector<long long>> dp(N + 1, vector<long long>(1 << N));
  dp[0][0] = 1;
  REP(i, N) {
    REP(j, (1 << N)) { // 立っている bit はマッチが終わっている
      if (i != __builtin_popcount(j))
        continue;
      REP(k, N) {
        if (!(j & (1 << k)) and a[i][k] == 1) {
          dp[i + 1][j + (1 << k)] += dp[i][j];
          dp[i + 1][j + (1 << k)] %= MOD;
        }
      }
    }
  }
  cout << dp[N][(1 << N) - 1] << endl;

  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Memory Limit Exceeded):
/*There's a possibility*/
#define dbg(...) ;
#define db(...) ;
#include "bits/stdc++.h"
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define f(i, a, b) for (i = a; i < b; i++)
#define fr(i, a, b) for (i = a; i >= b; i--)
#define endl '\n'
#define tst cout << '*';
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mod 1000000007
using namespace std;

ll n, ar[23][23], dp[23][1 << 23];

ll fun(int i, ll mask) {
  // dbg(i,mask);
  if (dp[i][mask] != -1)
    return dp[i][mask];
  if (i == n) {
    if (mask == ((1 << n) - 1))
      return dp[i][mask] = 1;
    else
      return dp[i][mask] = 0;
  }

  ll ans = 0, j;
  f(j, 0, n) {
    if (ar[i][j + 1] and !(mask & (1 << j)))
      ans = (ans + fun(i + 1, mask | (1 << j))) % mod;
  }
  return dp[i][mask] = ans;
}

int main() {
  fast;
  int i, j;
  cin >> n;
  f(i, 0, n) f(j, 1, n + 1) cin >> ar[i][j];
  memset(dp, -1, sizeof(dp));
  cout << fun(0, 0);
  // db(dp,0,n,0,3);
}

Fixed submission:
/*There's a possibility*/
#define dbg(...) ;
#define db(...) ;
#include "bits/stdc++.h"
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define f(i, a, b) for (i = a; i < b; i++)
#define fr(i, a, b) for (i = a; i >= b; i--)
#define endl '\n'
#define tst cout << '*';
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mod 1000000007
using namespace std;

ll n, ar[22][22], dp[22][1 << 22];

ll fun(int i, ll mask) {
  // dbg(i,mask);
  if (dp[i][mask] != -1)
    return dp[i][mask];
  if (i == n) {
    if (mask == ((1 << n) - 1))
      return dp[i][mask] = 1;
    else
      return dp[i][mask] = 0;
  }

  ll ans = 0, j;
  f(j, 0, n) {
    if (ar[i][j + 1] and !(mask & (1 << j)))
      ans = (ans + fun(i + 1, mask | (1 << j))) % mod;
  }
  return dp[i][mask] = ans;
}

int main() {
  fast;
  int i, j;
  cin >> n;
  f(i, 0, n) f(j, 1, n + 1) cin >> ar[i][j];
  memset(dp, -1, sizeof(dp));
  cout << fun(0, 0);
  // db(dp,0,n,0,3);
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define pb push_back
#define size(v) (int)v.size()
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
ll power_mod(ll a, ll x) {
  if (x == 0)
    return 1;
  ll y = power_mod(a, x / 2);
  ll ans = (y * y) % mod;
  if (x % 2)
    ans = (ans * a) % mod;
  return ans;
}
ll inv(ll a) { return power_mod(a, mod - 2); }
ll power(ll a, ll x) {
  if (x == 0)
    return 1;
  ll y = power(a, x / 2);
  ll ans = (y * y);
  if (x % 2)
    ans *= a;
  return ans;
}
ll dp[(1 << 16) + 3];
int main() {

  fast;
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }

  vector<int> dp(1 << n, 0);

  dp[0] = 1;

  for (int mask = 0; mask < (1 << n); mask++) {
    int count_one = __builtin_popcount(mask);

    for (int j = 0; j < n; j++) {
      if (a[count_one][j]) {
        if (!(mask & (1 << j))) {
          dp[mask + (1 << j)] = (dp[mask + (1 << j)] + dp[mask]) % mod;
        }
      }
    }
  }

  cout << dp[(1 << n) - 1] << endl;
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define pb push_back
#define size(v) (int)v.size()
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
ll power_mod(ll a, ll x) {
  if (x == 0)
    return 1;
  ll y = power_mod(a, x / 2);
  ll ans = (y * y) % mod;
  if (x % 2)
    ans = (ans * a) % mod;
  return ans;
}
ll inv(ll a) { return power_mod(a, mod - 2); }
ll power(ll a, ll x) {
  if (x == 0)
    return 1;
  ll y = power(a, x / 2);
  ll ans = (y * y);
  if (x % 2)
    ans *= a;
  return ans;
}
ll dp[(1 << 16) + 3];
int main() {

  fast;
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }

  vector<int> dp(1 << n, 0);

  dp[0] = 1;

  for (int mask = 0; mask < ((1 << n) - 1); mask++) {
    int count_one = __builtin_popcount(mask);

    for (int j = 0; j < n; j++) {
      if (a[count_one][j]) {
        if (!(mask & (1 << j))) {
          dp[mask + (1 << j)] = (dp[mask + (1 << j)] + dp[mask]) % mod;
        }
      }
    }
  }

  cout << dp[(1 << n) - 1] << endl;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;

int add(int a, int b) { return a + b - mod * (a + b >= mod); }

int mul(int a, int b) { return int(1ll * a * b % mod); }

int pwr(int a, int x) {
  if (x == 0) {
    return 1;
  }
  return mul(pwr(mul(a, a), x >> 1), (x & 1 ? a : 1));
}

int dvd(int a, int b) { return mul(a, pwr(b, mod - 2)); }

int sub(int a, int b) { return a - b + mod * (a < b); }

void inc(int &a, int b) { a = add(a, b); }

void dec(int &a, int b) { a = sub(a, b); }

const int N = 21;
int a[N][N], dp[1 << N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> a[i][j];
    }
  }
  dp[0] = 1;
  for (int i = 0; i < n; ++i) {
    for (int mask = (1 << n) - 1; mask >= 0; --mask) {
      for (int j = 0; j < n; ++j) {
        if (((mask >> j) & 1) == 0 and a[i][j]) {
          inc(dp[mask | (1 << j)], dp[mask]);
        }
      }
    }
  }
  cout << dp[(1 << n) - 1];

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;

int add(int a, int b) { return a + b - mod * (a + b >= mod); }

int mul(int a, int b) { return int(1ll * a * b % mod); }

int pwr(int a, int x) {
  if (x == 0) {
    return 1;
  }
  return mul(pwr(mul(a, a), x >> 1), (x & 1 ? a : 1));
}

int dvd(int a, int b) { return mul(a, pwr(b, mod - 2)); }

int sub(int a, int b) { return a - b + mod * (a < b); }

void inc(int &a, int b) { a = add(a, b); }

void dec(int &a, int b) { a = sub(a, b); }

const int N = 21;
int a[N][N], dp[1 << N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> a[i][j];
    }
  }
  dp[0] = 1;
  for (int mask = 0; mask < (1 << n); ++mask) {
    int j = __builtin_popcount(mask);
    for (int i = 0; i < n; ++i) {
      if (((mask >> i) & 1) == 0 and a[j][i]) {
        inc(dp[mask | (1 << i)], dp[mask]);
      }
    }
  }
  cout << dp[(1 << n) - 1];

  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
int N;
int a[23][23];
int A[23];
const ll MOD = 1e9 + 7;
ll dp[23][1 << 23];
ll mmax;
ll rec(int n, int m) {

  if (dp[n][m] != -1)
    return dp[n][m];
  if (n == 0) {
    if (m == mmax)
      return 1;
    else
      return 0;
  }
  ll res = 0;
  for (int i = 0; i < N; i++) {
    if (A[n - 1] >> i & 1)
      if (!(m >> i & 1))
        res += rec(n - 1, m + (1 << i)) % MOD;
  }
  return dp[n][m] = res % MOD;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      cin >> a[i][j];
  memset(dp, -1, sizeof(dp));
  memset(A, 0, sizeof(A));

  mmax = (1 << N) - 1;

  for (int i = N; 0 < i; i--) {

    for (int j = 0; j < N; j++)
      A[j] += (1 << N - i) * a[i - 1][j];
  }

  cout << rec(N, 0) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
int N;
int a[23][23];
int A[23];
const ll MOD = 1e9 + 7;
ll dp[23][1 << 22];
ll mmax;
ll rec(int n, int m) {

  if (dp[n][m] != -1)
    return dp[n][m];
  if (n == 0) {
    if (m == mmax)
      return 1;
    else
      return 0;
  }
  ll res = 0;
  for (int i = 0; i < N; i++) {
    if (A[n - 1] >> i & 1)
      if (!(m >> i & 1))
        res += rec(n - 1, m + (1 << i)) % MOD;
  }
  return dp[n][m] = res % MOD;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      cin >> a[i][j];
  memset(dp, -1, sizeof(dp));
  memset(A, 0, sizeof(A));

  mmax = (1 << N) - 1;

  for (int i = N; 0 < i; i--) {

    for (int j = 0; j < N; j++)
      A[j] += (1 << N - i) * a[i - 1][j];
  }

  cout << rec(N, 0) << endl;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const int MOD = 1e9 + 7;
int N;
int a[22][22];
ll dp[22][2200000];

int bitcnt(int n) {
  int ret = 0;
  for (int i = 0; i < 22; i++)
    if (n & (1 << i))
      ret++;
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      cin >> a[i][j];

  dp[0][0] = 1;
  for (int mask = 0; mask < (1 << N); mask++) {
    int i = bitcnt(mask);
    for (int k = 0; k < N; k++)
      dp[i + 1][mask | (1 << k)] =
          (dp[i + 1][mask | (1 << k)] + dp[i][mask] * a[i][k]) % MOD;
  }
  cout << dp[N][(1 << N) - 1] << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const int MOD = 1e9 + 7;
int N;
int a[22][22];
ll dp[23][2200000];

int bitcnt(int n) {
  int ret = 0;
  for (int i = 0; i < 22; i++)
    if (n & (1 << i))
      ret++;
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      cin >> a[i][j];

  dp[0][0] = 1;
  for (int mask = 0; mask < (1 << N); mask++) {
    int i = bitcnt(mask);
    for (int k = 0; k < N; k++)
      dp[i + 1][mask | (1 << k)] =
          (dp[i + 1][mask | (1 << k)] + dp[i][mask] * a[i][k]) % MOD;
  }
  cout << dp[N][(1 << N) - 1] << '\n';
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#define llint long long
#define mod 1000000007

using namespace std;

llint n;
llint a[25][25];
llint dp[25][1 << 21];

int main(void) {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }

  llint S = 1 << n;
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < S; j++) {
      for (int k = 0; k < n; k++) {
        if (j & (1 << k))
          continue;
        if (a[i + 1][k] == 0)
          continue;
        dp[i + 1][j | (1 << k)] += dp[i][j];
        dp[i + 1][j | (1 << k)] %= mod;
      }
    }
  }
  cout << dp[n][S - 1] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#define llint long long
#define mod 1000000007

using namespace std;

llint n;
llint a[25][25];
llint dp[25][1 << 21];

int main(void) {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }

  llint S = 1 << n;
  dp[0][0] = 1;
  for (int j = 0; j < S; j++) {
    int i = 0;
    for (int k = 0; k < n; k++)
      if (j & (1 << k))
        i++;
    for (int k = 0; k < n; k++) {
      if (j & (1 << k))
        continue;
      if (a[i + 1][k] == 0)
        continue;
      dp[i + 1][j | (1 << k)] += dp[i][j];
      dp[i + 1][j | (1 << k)] %= mod;
    }
  }
  cout << dp[n][S - 1] << endl;
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define MOD ll(1e9 + 7)
vector<vector<ll>> memo(22, vector<ll>(2e5 + 1e5, -1));

int binpow(int a, int b) {
  int res = 1;
  while (b) {
    if (b % 2)
      res = res * a;
    a *= a;
    b >>= 1;
  }
  return res;
}

ll dp(vector<vector<int>> &v, int mmask, int i) {
  if (i == v.size())
    return 1;
  if (memo[i][mmask] != -1)
    return memo[i][mmask];
  memo[i][mmask] = 0;
  for (int e : v[i]) {
    if (mmask & binpow(2, e)) {
      memo[i][mmask] =
          (memo[i][mmask] +
           dp(v, mmask & (binpow(2, 21) - 1 - binpow(2, e)), i + 1)) %
          MOD;
    }
  }
  return memo[i][mmask];
}

signed main() {
  cin.tie(0);
  cin.sync_with_stdio();
  int n;
  cin >> n;
  vector<vector<int>> v(n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      cin >> a;
      if (a)
        v[i].push_back(j);
    }
  }
  cout << dp(v, binpow(2, 21) - 1, 0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define MOD ll(1e9 + 7)
vector<vector<ll>> memo(22, vector<ll>(2e6 + 1e5, -1));

int binpow(int a, int b) {
  int res = 1;
  while (b) {
    if (b % 2)
      res = res * a;
    a *= a;
    b >>= 1;
  }
  return res;
}

ll dp(vector<vector<int>> &v, int mmask, int i) {
  if (i == v.size())
    return 1;
  if (memo[i][mmask] != -1)
    return memo[i][mmask];
  memo[i][mmask] = 0;
  for (int e : v[i]) {
    if (mmask & binpow(2, e)) {
      memo[i][mmask] =
          (memo[i][mmask] +
           dp(v, mmask & (binpow(2, 21) - 1 - binpow(2, e)), i + 1)) %
          MOD;
    }
  }
  return memo[i][mmask];
}

signed main() {
  cin.tie(0);
  cin.sync_with_stdio();
  int n;
  cin >> n;
  vector<vector<int>> v(n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      cin >> a;
      if (a)
        v[i].push_back(j);
    }
  }
  cout << dp(v, binpow(2, 21) - 1, 0);
}