Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long int // comment for large arrays
#define pll pair<int, int>
#define dbl long double
#define ff first
#define ss second
#define endl "\n"
#define mod 1000000007
#define eps 0.00000001
#define INF 10000000000000001
#define all(x) (x).begin(), (x).end()
#define LB(v, x) (lower_bound(all(v), x) - v.begin())
#define UB(v, x) (upper_bound(all(v), x) - v.begin())
#define size(x) (int)(x).size()
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define popb() pop_back()
#define popf() pop_front()
#define mp(x, y) make_pair((x), (y))
#define vec(dt) vector<dt>
#define vv(dt) vector<vector<dt>>
#define fastio(x)                                                              \
  ios_base::sync_with_stdio(x);                                                \
  cin.tie(NULL)
#define init(v, s) memset(v, s, sizeof(v))
#define bug(x)                                                                 \
  cerr << "LINE: " << __LINE__ << " || click to see test details " << #x       \
       << " = " << x << endl
#define loop(i, s, n) for (int i = s; i < n; i++)
#define print(v)                                                               \
  for (auto it : v)                                                            \
    cout << it << " ";                                                         \
  cout << endl
using namespace std;

signed main() {
  fastio(0);
  int n;
  cin >> n;
  vec(int) dp(1 << n, 0), a[n];
  loop(i, 0, n) loop(j, 0, n) {
    int x;
    cin >> x;
    if (x == 1)
      a[i].pb(j);
  }
  for (auto j : a[0])
    dp[1 << j] = 1;
  loop(i, 1, n) {
    for (int mask = (1 << n) - 1; mask >= 0; mask--) {
      for (auto j : a[i])
        if (mask & (1 << j))
          dp[mask] += dp[mask - (1 << j)];
      dp[mask] %= mod;
    }
  }
  cout << dp[(1 << n) - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long int // comment for large arrays
#define pll pair<int, int>
#define dbl long double
#define ff first
#define ss second
#define endl "\n"
#define mod 1000000007
#define eps 0.00000001
#define INF 10000000000000001
#define all(x) (x).begin(), (x).end()
#define LB(v, x) (lower_bound(all(v), x) - v.begin())
#define UB(v, x) (upper_bound(all(v), x) - v.begin())
#define size(x) (int)(x).size()
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define popb() pop_back()
#define popf() pop_front()
#define mp(x, y) make_pair((x), (y))
#define vec(dt) vector<dt>
#define vv(dt) vector<vector<dt>>
#define fastio(x)                                                              \
  ios_base::sync_with_stdio(x);                                                \
  cin.tie(NULL)
#define init(v, s) memset(v, s, sizeof(v))
#define bug(x)                                                                 \
  cerr << "LINE: " << __LINE__ << " || click to see test details " << #x       \
       << " = " << x << endl
#define loop(i, s, n) for (int i = s; i < n; i++)
#define print(v)                                                               \
  for (auto it : v)                                                            \
    cout << it << " ";                                                         \
  cout << endl
using namespace std;

signed main() {
  fastio(0);
  int n;
  cin >> n;
  vec(int) dp(1 << n, 0), a[n];
  loop(i, 0, n) loop(j, 0, n) {
    int x;
    cin >> x;
    if (x == 1)
      a[i].pb(j);
  }
  dp[0] = 1;
  loop(mask, 0, (1 << n) - 1) {
    int i = __builtin_popcount(mask);
    for (auto j : a[i])
      if ((mask & (1 << j)) == 0)
        dp[mask + (1 << j)] = (dp[mask + (1 << j)] + dp[mask]) % mod;
  }
  cout << dp[(1 << n) - 1] << endl;
  return 0;
}