Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define MuhammedAly                                                            \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define pb push_back
#define eb emplace_back
#define popc(x) __builtin_popcount(x)
#define LLMX 1e18
#define NotinRange(n, m) i < 0 || i >= (n) || j < 0 || j >= (m)
#define mem(x, y) memset((x), (y), sizeof(x));
#define sz(a) (int)(a).size()
#define inFile(s) freopen((s), "r", stdin)
#define outFile(s) freopen((s), "w", stdout)
#define biEdge(v, a, b) (v)[(a)].pb((b)), (v)[(b)].pb((a))
#define pi (2 * acos(0))
#define X real()
#define Y imag()
#define cross(a, b) ((conj(a) * (b)).Y)
#define dot(a, b) ((conj(a) * (b)).X)
#define vec(a, b) ((b) - (a))
#define ll long long
typedef long double ld;
typedef complex<double> point;
typedef tuple<int, int, int> line;
typedef pair<point, point> segment;
typedef vector<point> polygon;
const int N = 21, M = 100 + 5, OO = 0x3f3f3f3f, mod = 1e9 + 7, base = 131,
          mod2 = 1000136437, mod3 = 998244353;
const double EPS = 1e-9;

int n, memo[23][(1 << 21) + 2];
bool comp[23][23];

int solve(int i = 0, int mask = 0) {
  if (mask == (1 << n) - 1)
    return 1;
  if (i == n)
    return 0;
  int &ret = memo[i][mask];
  if (~ret)
    return ret;
  ret = solve(i + 1, mask);
  for (int j = 0; j < n; j++)
    if (comp[i][j] && !((mask >> j) & 1))
      ret = (ret + solve(i + 1, (mask | (1 << j)))) % mod;
  return ret % mod;
}
int main() {
  MuhammedAly mem(memo, -1);
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> comp[i][j];
  cout << solve();
  return 0;
}


Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define MuhammedAly                                                            \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define pb push_back
#define eb emplace_back
#define popc(x) __builtin_popcount(x)
#define LLMX 1e18
#define NotinRange(n, m) i < 0 || i >= (n) || j < 0 || j >= (m)
#define mem(x, y) memset((x), (y), sizeof(x));
#define sz(a) (int)(a).size()
#define inFile(s) freopen((s), "r", stdin)
#define outFile(s) freopen((s), "w", stdout)
#define biEdge(v, a, b) (v)[(a)].pb((b)), (v)[(b)].pb((a))
#define pi (2 * acos(0))
#define X real()
#define Y imag()
#define cross(a, b) ((conj(a) * (b)).Y)
#define dot(a, b) ((conj(a) * (b)).X)
#define vec(a, b) ((b) - (a))
#define ll long long
typedef long double ld;
typedef complex<double> point;
typedef tuple<int, int, int> line;
typedef pair<point, point> segment;
typedef vector<point> polygon;
const int N = 21, M = 100 + 5, OO = 0x3f3f3f3f, mod = 1e9 + 7, base = 131,
          mod2 = 1000136437, mod3 = 998244353;
const double EPS = 1e-9;

int n, memo[23][(1 << 21) + 2];
bool comp[23][23];

int solve(int i = 0, int mask = 0) {
  if (mask == (1 << n) - 1)
    return 1;
  if (i == n)
    return 0;
  int &ret = memo[i][mask];
  if (~ret)
    return ret;
  ret = 0;
  for (int j = 0; j < n; j++)
    if (comp[i][j] && !((mask >> j) & 1))
      ret = (ret + solve(i + 1, (mask | (1 << j)))) % mod;
  return ret % mod;
}
int main() {
  MuhammedAly mem(memo, -1);
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> comp[i][j];
  cout << solve();
  return 0;
}