Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fastio                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
// STANDARD DATA TYPES
#define ll long long
#define llu unsigned long long
#define ld long double
// ADV DATA TYPES
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define ff first
#define ss second
// C ++ DS
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define pf push_front
#define iter ::iterator
#define all(x) x.begin(), x.end()

// priority_queue < pii, vector < pii >, greater < pii > > pq;
// lower_bound(v.begin(),v.end(),20);//for any sorted container
// CONSTANTS
#define MOD (int)(1e9 + 7)
#define INF 0x3f3f3f3f
#define MAXN (int)(1e5 + 1)
// MOD OPERATIONS
inline ll fpow(ll n, ll k, int p = MOD) {
  ll r = 1;
  for (; k; k >>= 1) {
    if (k & 1)
      r = r * n % p;
    n = n * n % p;
  }
  return r;
}
inline ll inv(ll a, ll p = MOD) { return fpow(a, p - 2, p); }
inline ll addmod(ll a, ll val, ll p = MOD) {
  {
    if ((a = (a + val)) >= p)
      a -= p;
  }
  return a;
}
inline ll submod(ll a, ll val, ll p = MOD) {
  {
    if ((a = (a - val)) < 0)
      a += p;
  }
  return a;
}
inline ll mult(ll a, ll b, ll p = MOD) { return (ll)a * b % p; }
// DEBUG
#define shout()                                                                \
  { cout << "I'm Here...!!!" << endl; }
#define dbg(x)                                                                 \
  { cout << #x << ": " << (x) << endl; }
#define dbg2(x, y)                                                             \
  { cout << #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
int main() {
  fastio;
  int n;
  cin >> n;
  vector<vi> match(n, vi(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> match[i][j];
  }
  // dp[i][mask] represent number of ways to match subset women(mask) with upto
  // 0 to i men for i+1 iterate over masks then jth women if jth women can pair
  // with ith men dp[i+1][mask^(1<<j)]+=dp[i][mask] else
  // dp[i+1][mask]+=dp[i][mask]//dont pair ith men(note if we iterate mask big
  // to small then this will be first update of dp[i+1][mask])
  vector<int> dp(1 << n);
  dp[0] = 1;
  for (int i = 0; i < n; i++) // ith men
  {
    for (int mask = (1 << n) - 1; mask >= 0; mask--) {
      for (int j = 0; j < n; j++) {
        if (match[i][j] && !(mask & (1 << j))) {
          dp[mask ^ (1 << j)] = addmod(dp[mask ^ (1 << j)], dp[mask]);
        }
      }
    }
  }
  cout << dp[(1 << n) - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fastio                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
// STANDARD DATA TYPES
#define ll long long
#define llu unsigned long long
#define ld long double
// ADV DATA TYPES
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define ff first
#define ss second
// C ++ DS
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define pf push_front
#define iter ::iterator
#define all(x) x.begin(), x.end()

// priority_queue < pii, vector < pii >, greater < pii > > pq;
// lower_bound(v.begin(),v.end(),20);//for any sorted container
// CONSTANTS
#define MOD (int)(1e9 + 7)
#define INF 0x3f3f3f3f
#define MAXN (int)(1e5 + 1)
// MOD OPERATIONS
inline ll fpow(ll n, ll k, int p = MOD) {
  ll r = 1;
  for (; k; k >>= 1) {
    if (k & 1)
      r = r * n % p;
    n = n * n % p;
  }
  return r;
}
inline ll inv(ll a, ll p = MOD) { return fpow(a, p - 2, p); }
inline ll addmod(ll a, ll val, ll p = MOD) {
  {
    if ((a = (a + val)) >= p)
      a -= p;
  }
  return a;
}
inline ll submod(ll a, ll val, ll p = MOD) {
  {
    if ((a = (a - val)) < 0)
      a += p;
  }
  return a;
}
inline ll mult(ll a, ll b, ll p = MOD) { return (ll)a * b % p; }
// DEBUG
#define shout()                                                                \
  { cout << "I'm Here...!!!" << endl; }
#define dbg(x)                                                                 \
  { cout << #x << ": " << (x) << endl; }
#define dbg2(x, y)                                                             \
  { cout << #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
int main() {
  fastio;
  int n;
  cin >> n;
  vector<vi> match(n, vi(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> match[i][j];
  }
  // dp[i][mask] represent number of ways to match subset women(mask) with upto
  // 0 to i men for i+1 iterate over masks then jth women if jth women can pair
  // with ith men dp[i+1][mask^(1<<j)]+=dp[i][mask] else
  // dp[i+1][mask]+=dp[i][mask]//dont pair ith men(note if we iterate mask big
  // to small then this will be first update of dp[i+1][mask])
  vector<int> dp(1 << n);
  dp[0] = 1;
  for (int i = 0; i < n; i++) // ith men
  {
    for (int mask = (1 << n) - 1; mask >= 0; mask--) {
      if (dp[mask] == 0)
        continue;
      for (int j = 0; j < n; j++) {
        if (match[i][j] && !(mask & (1 << j))) {
          dp[mask ^ (1 << j)] = addmod(dp[mask ^ (1 << j)], dp[mask]);
        }
      }
    }
  }
  cout << dp[(1 << n) - 1] << endl;
}