Problem p03174 (C++)

Buggy submission (status: Memory Limit Exceeded):
/*
        HELLO WORLD
        -----------
        SHUBHAM
        -----------
        :) CODING
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ff first
#define ss second
#define pb push_back
#define int long long
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define umii unordered_map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n]
#define test(t)                                                                \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define SIZE 100005
#define all(x) (x).begin(), (x).end()
#define sclk() auto start_time = Clock::now()
#define eclk()                                                                 \
  auto end_time = Clock::now();                                                \
  cout << "\nTime Taken : "                                                    \
       << chrono::duration_cast<chrono::milliseconds>(end_time - start_time)   \
              .count()                                                         \
       << " ms"
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef std::chrono::high_resolution_clock Clock;

int powmod(int a, int b) {
  int res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

void f_io() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
int dp[22][1 << 23];
int arr[21][21];
int n;
int solve(int r, int s) {
  if (r == n) {
    if (s == 0)
      return 1;
    return 0;
  }
  if (dp[r][s] == -1) {
    int ans = 0;
    for (int i = 0; i < n; i++) {
      int ck = (1 << i) & s;
      if (arr[r][i] and ck) {
        ans = (ans + solve(r + 1, s ^ (1 << i))) % mod;
      }
    }
    dp[r][s] = ans;
  }
  return dp[r][s];
}
int32_t main() {
  memset(dp, -1, sizeof dp);
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> arr[i][j];
    }
  }
  int s = (1 << n) - 1;
  cout << solve(0, s);
  return 0;
}


Fixed submission:
/*
        HELLO WORLD
        -----------
        SHUBHAM
        -----------
        :) CODING
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ff first
#define ss second
#define pb push_back
// #define int                                        long long
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define umii unordered_map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n]
#define test(t)                                                                \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define SIZE 100005
#define all(x) (x).begin(), (x).end()
#define sclk() auto start_time = Clock::now()
#define eclk()                                                                 \
  auto end_time = Clock::now();                                                \
  cout << "\nTime Taken : "                                                    \
       << chrono::duration_cast<chrono::milliseconds>(end_time - start_time)   \
              .count()                                                         \
       << " ms"
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef std::chrono::high_resolution_clock Clock;

int powmod(int a, int b) {
  int res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

void f_io() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
int dp[22][1 << 23];
int arr[21][21];
int n;
int solve(int r, int s) {
  if (r == n) {
    if (s == 0)
      return 1;
    return 0;
  }
  if (dp[r][s] == -1) {
    int ans = 0;
    for (int i = 0; i < n; i++) {
      int ck = (1 << i) & s;
      if (arr[r][i] and ck) {
        ans = (ans + solve(r + 1, s ^ (1 << i))) % mod;
      }
    }
    dp[r][s] = ans;
  }
  return dp[r][s];
}
int32_t main() {
  memset(dp, -1, sizeof dp);
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> arr[i][j];
    }
  }
  int s = (1 << n) - 1;
  cout << solve(0, s);
  return 0;
}