Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef vector<int> vi;
// typedef vector<l>vl ;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vstr;
void READ(vll &v, ll n) {
  ll a;
  for (ll i = 0; i < n; i = i + 1) {
    cin >> a;
    v.pb(a);
  }
}
void PRINT(vll &v, ll n) {
  ll a;
  for (ll i = 0; i < n; i = i + 1) {
    cout << v[i] << " ";
  }
  cout << "\n";
}
ll n, m, k;
ll dp[23][2097160];
ll v[23][23];
ll solve(ll i, ll j) {
  if (i >= n && j == k) {
    return (1);
  }
  if (i == n) {
    return (0);
  }
  if (dp[i][j] != -1) {
    return (dp[i][j]);
  }
  ll ans = 0;
  for (ll m = 0; m < n; m = m + 1) {
    if (v[i][m] == 1 && (j ^ (1 << m))) {
      ans = ans + solve(i + 1, j ^ (1 << m));
      ans %= mod;
    }
  }
  dp[i][j] = ans;
  return (ans);
}
int main() {
  ios_base::sync_with_stdio(
      false); // this and next two lines for cin cout TLE handling
  cin.tie(NULL);
  cout.tie(NULL);
  std::cout.unsetf(
      std::ios::floatfield); // this and next line for floating precision upto 6
  std::cout.precision(6);
  ll a, b, c, d, e, x, i, j, t, y, w;
  double p, q, r, u, s;
  string str = "", str1 = "";
  /*char ch,chr ;
  ll a1=0,a2=0,b1=0,b2=0 ;*/
  cin >> n;
  k = (1 << n) - 1;
  for (i = 0; i < n + 2; i = i + 1) {
    for (j = 0; j < k + 2; j = j + 1) {
      dp[i][j] = -1;
    }
  }
  for (i = 0; i < n; i = i + 1) {
    for (j = 0; j < n; j = j + 1) {
      cin >> v[i][j];
    }
  }
  a = solve(0, 0);
  cout << a;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef vector<int> vi;
// typedef vector<l>vl ;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vstr;
void READ(vll &v, ll n) {
  ll a;
  for (ll i = 0; i < n; i = i + 1) {
    cin >> a;
    v.pb(a);
  }
}
void PRINT(vll &v, ll n) {
  ll a;
  for (ll i = 0; i < n; i = i + 1) {
    cout << v[i] << " ";
  }
  cout << "\n";
}
ll n, m, k;
ll dp[23][2097160];
ll v[23][23];
ll solve(ll i, ll j) {
  if (i >= n && j == k) {
    return (1);
  }
  if (i == n) {
    return (0);
  }
  if (dp[i][j] != -1) {
    return (dp[i][j]);
  }
  ll ans = 0;
  for (ll m = 0; m < n; m = m + 1) {
    if (v[i][m] == 1 && !(j & (1 << m))) {
      ans = ans + solve(i + 1, j ^ (1 << m));
      ans %= mod;
    }
  }
  dp[i][j] = ans;
  return (ans);
}
int main() {
  ios_base::sync_with_stdio(
      false); // this and next two lines for cin cout TLE handling
  cin.tie(NULL);
  cout.tie(NULL);
  std::cout.unsetf(
      std::ios::floatfield); // this and next line for floating precision upto 6
  std::cout.precision(6);
  ll a, b, c, d, e, x, i, j, t, y, w;
  double p, q, r, u, s;
  string str = "", str1 = "";
  /*char ch,chr ;
  ll a1=0,a2=0,b1=0,b2=0 ;*/
  cin >> n;
  k = (1 << n) - 1;
  for (i = 0; i < n + 2; i = i + 1) {
    for (j = 0; j < k + 2; j = j + 1) {
      dp[i][j] = -1;
    }
  }
  for (i = 0; i < n; i = i + 1) {
    for (j = 0; j < n; j = j + 1) {
      cin >> v[i][j];
    }
  }
  a = solve(0, 0);
  cout << a;
}