Problem p03174 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
#define CLK clock_t clk = clock(); // Start of main
#define OCLK                                                                   \
  cerr << "Time (in ms): "                                                     \
       << (double)(clock() - clk) * 1000.0 / CLOCKS_PER_SEC                    \
       << '\n'; // End of main
#define MOD 1000000007
/* DEFINE cout */
#define pln cout << "===============================\n"
#define on cout << "\n"
#define os cout << " "
#define o2(a, b) cout << a << " " << b
#define o(a) cout << a
#define bitcount __builtin_popcount
/* DEFINE cout */
#define endl "\n"
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0); // fastio
using namespace std;
struct hash_pair { // use pair as key in
                   // unordered_map<pair<int,int>,int,hash_pair>;
  template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};
struct ArrayHasher {
  std::size_t operator()(const std::array<int, 3> &a) {
    std::size_t h = 0;

    for (auto e : a) {
      h ^= std::hash<int>{}(e) + 0x9e3779b9 + (h << 6) + (h >> 2);
    }
    return h;
  }
};
bool prime[10000001];
int P = MOD - 2;
int factorial[10000001] = {0};
int powerFunction(int x, int y) {
  int res = 1;
  int p = MOD;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
void sieveFunction(int maxLimit) {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;
  for (int i = 2; maxLimit >= i; i++) {
    if (prime[i]) {
      for (int j = 2 * i; maxLimit >= j; j += i)
        prime[j] = false;
    }
  }
}
void factorialFunction(int maxLimit) {
  factorial[0] = 1;
  for (int i = 1; i <= maxLimit; i++)
    factorial[i] = (factorial[i - 1] * i) % MOD;
  return;
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int n;
int arr[22][22];
int has[22];
int dp[22][2 << 22];
int fun(int idx, int bh) {
  if (idx == n) {
    return 1;
  }
  if (dp[idx][bh] != -1) {
    return dp[idx][bh];
  }
  int temp = 0;
  for (int j = 0; n > j; j++) {
    if (arr[idx][j] == 1 && has[j] == 1) {
      has[j] = 0;
      bh += 2 << (j);
      temp += fun(idx + 1, bh);
      temp %= MOD;
      bh -= 2 << (j);
      has[j] = 1;
    }
  }
  dp[idx][bh] = temp;
  return temp;
}
signed main() {
  // CLK;
  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (int i = 0; n > i; i++) {
    has[i] = 1;
    for (int j = 0; n > j; j++) {
      cin >> arr[i][j];
    }
  }
  cout << fun(0, 0) << endl;
  // OCLK;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define CLK clock_t clk = clock(); // Start of main
#define OCLK                                                                   \
  cerr << "Time (in ms): "                                                     \
       << (double)(clock() - clk) * 1000.0 / CLOCKS_PER_SEC                    \
       << '\n'; // End of main
#define MOD 1000000007
/* DEFINE cout */
#define pln cout << "===============================\n"
#define on cout << "\n"
#define os cout << " "
#define o2(a, b) cout << a << " " << b
#define o(a) cout << a
#define bitcount __builtin_popcount
/* DEFINE cout */
#define endl "\n"
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0); // fastio
using namespace std;
struct hash_pair { // use pair as key in
                   // unordered_map<pair<int,int>,int,hash_pair>;
  template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};
struct ArrayHasher {
  std::size_t operator()(const std::array<int, 3> &a) {
    std::size_t h = 0;

    for (auto e : a) {
      h ^= std::hash<int>{}(e) + 0x9e3779b9 + (h << 6) + (h >> 2);
    }
    return h;
  }
};
bool prime[10000001];
int P = MOD - 2;
int factorial[10000001] = {0};
int powerFunction(int x, int y) {
  int res = 1;
  int p = MOD;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
void sieveFunction(int maxLimit) {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;
  for (int i = 2; maxLimit >= i; i++) {
    if (prime[i]) {
      for (int j = 2 * i; maxLimit >= j; j += i)
        prime[j] = false;
    }
  }
}
void factorialFunction(int maxLimit) {
  factorial[0] = 1;
  for (int i = 1; i <= maxLimit; i++)
    factorial[i] = (factorial[i - 1] * i) % MOD;
  return;
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int n;
int arr[22][22];
int has[22];
int dp[22][2 << 22];
int fun(int idx, int bh) {
  if (idx == n) {
    return 1;
  }
  if (dp[idx][bh] != -1) {
    return dp[idx][bh];
  }
  int temp = 0;
  for (int j = 0; n > j; j++) {
    if (arr[idx][j] == 1 && has[j] == 1) {
      has[j] = 0;
      bh += 2 << (j);
      temp += fun(idx + 1, bh);
      temp %= MOD;
      bh -= 2 << (j);
      has[j] = 1;
    }
  }
  dp[idx][bh] = temp;
  return temp;
}
signed main() {
  // CLK;
  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (int i = 0; n > i; i++) {
    has[i] = 1;
    for (int j = 0; n > j; j++) {
      cin >> arr[i][j];
    }
  }
  cout << fun(0, 0) << endl;
  // OCLK;
}