Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
#define db(x) cout << #x << "=" << x << '\n'
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << '\n'
#define db3(x, y, z)                                                           \
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << '\n'
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fr(i, a, n) for (int i = a; i <= (n); ++i)
#define rf(i, a, n) for (int i = a; i >= (n); --i)
#define mod 1000000007
#define eps 1e-9
#define PI 3.14159265358979323846
#define F first
#define S second
#define pb push_back
#define pf push_front
#define mp make_pair
#define pll pair<ll, ll>
#define _ << " " <<
using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<int, pair<int, int>> iii;
typedef vector<ii> vii;
typedef vector<iii> viii;

vector<bool> vis;
vvi dp;

ll matching(vvi a, int mask, int n) {
  if (n == 0)
    return 1;
  if (mask == 0)
    return 0;
  if (dp[n][mask] != -1)
    return dp[n][mask];
  ll val = 0;
  // db(mask);
  for (int i = 0; i < a.size(); i++) {
    // db2(a[n-1][i],(mask&(1<<i)));
    if (a[n - 1][i] && (mask & (1 << i))) {
      // db2(n,mask);
      val += matching(a, mask ^ (1 << i), n - 1);
      val %= mod;
    }
  }
  dp[n][mask] = val % mod;
  // db(val);
  return dp[n][mask];
}

int main() {
  int n;
  cin >> n;
  vvi a(n, vi(n, 0));
  int mask = 0;
  for (int i = 0; i < n; i++) {
    mask |= (1 << i);
    for (int j = 0; j < n; ++j) {
      cin >> a[i][j];
    }
  }
  dp.clear();
  dp.resize(n + 1, vi(1 << n, -1));
  cout << matching(a, mask, n);
  // 	dp.resize(n,vi(n,1e18));

  // for(int i=0;i<=n;i++){
  // 	for(int j=0;j<=n;j++){
  // 		if(i==j){
  // 			dp[i][j]=i;
  // 		}
  // 	}
  // }
  // 	cout<<dp[0][n-1]<<endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#define db(x) cout << #x << "=" << x << '\n'
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << '\n'
#define db3(x, y, z)                                                           \
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << '\n'
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fr(i, a, n) for (int i = a; i <= (n); ++i)
#define rf(i, a, n) for (int i = a; i >= (n); --i)
#define mod 1000000007
#define eps 1e-9
#define PI 3.14159265358979323846
#define F first
#define S second
#define pb push_back
#define pf push_front
#define mp make_pair
#define pll pair<ll, ll>
#define _ << " " <<
using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<int, pair<int, int>> iii;
typedef vector<ii> vii;
typedef vector<iii> viii;

vector<bool> vis;
vvi dp;

ll matching(vvi &a, int mask, int n) {
  if (n == 0)
    return 1;
  if (mask == 0)
    return 0;
  if (dp[n][mask] != -1)
    return dp[n][mask];
  ll val = 0;
  // db(mask);
  for (int i = 0; i < a.size(); i++) {
    // db2(a[n-1][i],(mask&(1<<i)));
    if (a[n - 1][i] && (mask & (1 << i))) {
      // db2(n,mask);
      val += matching(a, mask ^ (1 << i), n - 1);
      val %= mod;
    }
  }
  dp[n][mask] = val % mod;
  // db(val);
  return dp[n][mask];
}

int main() {
  int n;
  cin >> n;
  vvi a(n, vi(n, 0));
  int mask = 0;
  for (int i = 0; i < n; i++) {
    mask |= (1 << i);
    for (int j = 0; j < n; ++j) {
      cin >> a[i][j];
    }
  }
  dp.clear();
  dp.resize(n + 1, vi(1 << n, -1));
  cout << matching(a, mask, n);
  // 	dp.resize(n,vi(n,1e18));

  // for(int i=0;i<=n;i++){
  // 	for(int j=0;j<=n;j++){
  // 		if(i==j){
  // 			dp[i][j]=i;
  // 		}
  // 	}
  // }
  // 	cout<<dp[0][n-1]<<endl;
  return 0;
}