Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
// #############################################################################
#include "bits/stdc++.h"
// #############################################################################
// Macros
#define MOD 1000000007
#define INF 1000000000000009
#define eb emplace_back
#define mp make_pair
#define fr first
#define sc second
#define vec vector
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a, n) a, a + n
#define sum(v) accumulate(all(v), 0LL)
// Type Defs
typedef long long ll;
typedef unsigned long long ull;
// #############################################################################
using namespace std;
// #############################################################################
// Helpers
void io_handler() {
#ifdef LOCAL_TESTING
  freopen("inputf.in", "r", stdin);
  freopen("outputf.in", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
template <typename T> void print_matrix(vector<vector<T>> mat) {
  for (auto row : mat) {
    for (auto e : row) {
      // auto fst = e[0]==INF?"INF":to_string(e[0]);
      // auto scd = e[1]==INF?"INF":to_string(e[1]);
      // cout <<"["<<fst<<","<<scd<< "]"<<" ";
      cout << e << " ";
    }
    cout << endl;
  }
}
template <typename T> void print_vector(vector<T> v) {
  for (auto e : v) {
    cout << e << " ";
  }
  cout << endl;
}
template <typename T> map<T, T> get_count(vector<T> v) {
  map<T, T> count;
  for (auto e : v) {
    if (count.find(e) != count.end()) {
      count[e]++;
    } else {
      count[e] = 1;
    }
  }
  return count;
}
void add_self(ll &a, ll b) {
  a = a + b;
  if (a >= MOD) {
    a -= MOD;
  }
}
ll sub_mod(ll a, ll b) {
  a = (a - b + MOD) % MOD;
  return a;
}
// #############################################################################
// Solution
void solve() {
  ll n;
  cin >> n;
  vec<vec<ll>> match(n, vec<ll>(n, 0LL));
  for (ll i = 0LL; i < n; i++) {
    for (ll j = 0LL; j < n; j++) {
      cin >> match[i][j];
    }
  }
  ll mxN = (1LL << n);
  vec<vec<ll>> dp(n + 1, vec<ll>(mxN, 0LL));
  dp[0][0] = 1;
  for (ll i = 1LL; i <= n; i++) {
    for (ll mask = 0LL; mask < mxN; mask++) {
      for (ll j = 0LL; j < n; j++) {
        if (match[i - 1][j]) {
          ll sub_mask = 1 << j;
          if (!(mask & sub_mask)) {
            add_self(dp[i][mask | sub_mask], dp[i - 1][mask]);
          }
        }
      }
    }
  }
  // print_matrix(dp);
  cout << dp[n][mxN - 1] << endl;
}
// #############################################################################
// Driver
int main() {
  io_handler();
  int t = 1;
  // cin>>t;
  for (int i = 0; i < t; i++) {
    solve();
  }
#ifdef LOCAL_TESTING
  cout << "⏰ Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}
// #############################################################################


Fixed submission:
// #############################################################################
#include "bits/stdc++.h"
// #############################################################################
// Macros
#define MOD 1000000007
#define INF 1000000000000009
#define eb emplace_back
#define mp make_pair
#define fr first
#define sc second
#define vec vector
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a, n) a, a + n
#define sum(v) accumulate(all(v), 0LL)
// Type Defs
typedef long long ll;
typedef unsigned long long ull;
// #############################################################################
using namespace std;
// #############################################################################
// Helpers
void io_handler() {
#ifdef LOCAL_TESTING
  freopen("inputf.in", "r", stdin);
  freopen("outputf.in", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
template <typename T> void print_matrix(vector<vector<T>> mat) {
  for (auto row : mat) {
    for (auto e : row) {
      // auto fst = e[0]==INF?"INF":to_string(e[0]);
      // auto scd = e[1]==INF?"INF":to_string(e[1]);
      // cout <<"["<<fst<<","<<scd<< "]"<<" ";
      cout << e << " ";
    }
    cout << endl;
  }
}
template <typename T> void print_vector(vector<T> v) {
  for (auto e : v) {
    cout << e << " ";
  }
  cout << endl;
}
template <typename T> map<T, T> get_count(vector<T> v) {
  map<T, T> count;
  for (auto e : v) {
    if (count.find(e) != count.end()) {
      count[e]++;
    } else {
      count[e] = 1;
    }
  }
  return count;
}
void add_self(ll &a, ll b) {
  a = a + b;
  if (a >= MOD) {
    a -= MOD;
  }
}
ll sub_mod(ll a, ll b) {
  a = (a - b + MOD) % MOD;
  return a;
}
// #############################################################################
// Solution
void solve() {
  ll n;
  cin >> n;
  vec<vec<ll>> match(n, vec<ll>(n, 0LL));
  for (ll i = 0LL; i < n; i++) {
    for (ll j = 0LL; j < n; j++) {
      cin >> match[i][j];
    }
  }
  ll mxN = (1LL << n);
  vec<vec<ll>> dp(n + 1, vec<ll>(mxN, 0LL));
  dp[0][0] = 1;
  for (ll i = 1LL; i <= n; i++) {
    for (ll mask = 0LL; mask < mxN; mask++) {
      if (!dp[i - 1][mask]) {
        continue;
      }
      for (ll j = 0LL; j < n; j++) {
        if (match[i - 1][j]) {
          ll sub_mask = 1 << j;
          if (!(mask & sub_mask)) {
            add_self(dp[i][mask | sub_mask], dp[i - 1][mask]);
          }
        }
      }
    }
  }
  // print_matrix(dp);
  cout << dp[n][mxN - 1] << endl;
}
// #############################################################################
// Driver
int main() {
  io_handler();
  int t = 1;
  // cin>>t;
  for (int i = 0; i < t; i++) {
    solve();
  }
#ifdef LOCAL_TESTING
  cout << "⏰ Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}
// #############################################################################