Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm> //swap function is here
#include <bits/stdc++.h>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream> // istringstream buffer(myString);
#include <stack>
#include <vector>
using namespace std;

//__builtin_popcount(n);

#define bit(x, i) (x & (1 << i))          // select the bit of position i of x
#define lowbit(x) ((x) & ((x) ^ ((x)-1))) // get the lowest bit of x
#define hBit(msb, n)                                                           \
  asm("bsrl %1,%0"                                                             \
      : "=r"(msb)                                                              \
      : "r"(n)) // get the highest bit of x, maybe the fastest
#define max(a, b) (a < b ? b : a)

#define IN(i, l, r) (l < i && i < r) // the next for are for checking bound
#define LINR(i, l, r) (l <= i && i <= r)
#define LIN(i, l, r) (l <= i && i < r)
#define INR(i, l, r) (l < i && i <= r)
#define F(i, L, R) for (int i = L; i < R; i++) // next four are for "for loops"
#define FE(i, L, R) for (int i = L; i <= R; i++)
#define FF(i, L, R) for (int i = L; i > R; i--)
#define FFE(i, L, R) for (int i = L; i >= R; i--)
#define getI(a)                                                                \
  scanf("%d", &a) // next three are handy ways to get ints, it's also force you
                  // to use '&' sign
#define getII(a, b) scanf("%d%d", &a, &b)
#define getIII(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define wez(n)                                                                 \
  int(n);                                                                      \
  scanf(                                                                       \
      "%d",                                                                    \
      &(n)) // handy if the input is right after the definition of a variable
#define wez2(n, m)                                                             \
  int(n), (m);                                                                 \
  scanf("%d %d", &(n), &(m))
#define wez3(n, m, k)                                                          \
  int(n), (m), (k);                                                            \
  scanf("%d %d %d", &(n), &(m), &(k))
#define TESTS wez(testow) while (testow--) // for multilple cases problems
#define whileZ                                                                 \
  int T;                                                                       \
  getI(T);                                                                     \
  while (T--)                             // the same as above
#define getS(x) scanf("%s", x)            // get a char* string
#define clr(a, x) memset(a, x, sizeof(a)) // set array to some value
#define char2Int(c) (c - '0')
#define lastEle(vec) vec[vec.size() - 1]
#define SZ(x) ((int)((x).size()))
#define REMAX(a, b) (a) = max((a), (b)) // set a to the maximum of a and b
#define REMIN(a, b) (a) = min((a), (b));
#define FOREACH(i, t)                                                          \
  for (typeof(t.begin()) i = t.begin(); i != t.end();                          \
       i++)                           // traverse an STL data structure
#define ALL(c) (c).begin(), (c).end() // handy for function like "sort()"
#define PRESENT(c, x) ((c).find(x) != (c).end())
#define CPRESENT(c, x) (find(ALL(c), x) != (c).end())
#define ll                                                                     \
  long long // data types used often, but you don't want to type them time by
            // time
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define IOS                                                                    \
  ios_base::sync_with_stdio(0); // to synchronize the input of cin and scanf
#define INF 1001001001
#define PI 3.1415926535897932384626
// for map, pair
#define mp make_pair
#define fi first
#define se second
// for debug
inline void pisz(int n) { printf("%d\n", n); }
#define DBG(vari) cerr << #vari << " = " << (vari) << endl;
#define printA(a, L, R) FE(i, L, R) cout << a[i] << (i == R ? '\n' : ' ')
#define printV(a) printA(a, 0, a.size() - 1)
#define MAXN 10000
// for vectors
#define pb push_back
typedef int elem_t;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
// directions
const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0},  {-1, 0},
                       {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
template <typename T, typename TT>
ostream &operator<<(ostream &s, pair<T, TT> t) {
  return s << "(" << t.first << "," << t.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, vector<T> t) {
  F(i, 0, SZ(t)) s << t[i] << " ";
  return s;
}

ll mod = 1000000007;

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

ll power(ll a, ll b, ll m) {
  ll r = 1;
  a = a % m;
  while (b) {
    if (b & 1)
      r = r * a % m;
    a = a * a % m;
    b = b >> 1;
  }
  return r;
}
ll findlcm(int arr[], int n) {
  // Initialize result
  ll ans = arr[0];

  // ans contains LCM of arr[0], ..arr[i]
  // after i'th iteration,
  for (int i = 1; i < n; i++)
    ans = (((arr[i] * ans)) / (gcd(arr[i], ans)));

  return ans;
}

ll count = 0;
ll dp[(1 << 21)][22];

ll recur(vector<std::vector<int>> matrix, int mask, int index) {
  // cout << matrix << "::\n";
  if (mask == (1 << (int)(matrix.size())) - 1 && index == (int)matrix.size()) {
    // cout << "aya\n";
    return 1;
  }
  if (dp[mask][index] != -1)
    return dp[mask][index];
  if (index == (int)matrix.size())
    return 0;

  dp[mask][index] = 0;
  for (int i = 0; i < matrix.size(); ++i) { // bool test = mask & (1 << i);

    // cout << test << "::\n";
    if (((mask & (1 << i)) == 0) && matrix[index][i] == 1) {
      // cout << "tata: " << mask << " toto: " << matrix[index][i] << "\n";
      int temp = mask | (1 << i);
      dp[mask][index] =
          (dp[mask][index] + recur(matrix, temp, index + 1)) % mod;
    }
    /* code */
  }

  return dp[mask][index];
}

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n;
  cin >> n;
  std::vector<std::vector<int>> matrix;
  for (int i = 0; i < n; ++i) {
    std::vector<int> temp;
    for (int j = 0; j < n; ++j) {
      int val;
      cin >> val;
      temp.push_back(val);
      /* code */
    }
    matrix.push_back(temp);
    /* code */
  }
  // cout << matrix << "\n";
  int mask = 0;
  memset(dp, -1, sizeof(dp));
  recur(matrix, mask, 0);

  cout << dp[0][0] << "\n";
}

Fixed submission:
#include <algorithm> //swap function is here
#include <bits/stdc++.h>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream> // istringstream buffer(myString);
#include <stack>
#include <vector>
using namespace std;

//__builtin_popcount(n);

#define bit(x, i) (x & (1 << i))          // select the bit of position i of x
#define lowbit(x) ((x) & ((x) ^ ((x)-1))) // get the lowest bit of x
#define hBit(msb, n)                                                           \
  asm("bsrl %1,%0"                                                             \
      : "=r"(msb)                                                              \
      : "r"(n)) // get the highest bit of x, maybe the fastest
#define max(a, b) (a < b ? b : a)

#define IN(i, l, r) (l < i && i < r) // the next for are for checking bound
#define LINR(i, l, r) (l <= i && i <= r)
#define LIN(i, l, r) (l <= i && i < r)
#define INR(i, l, r) (l < i && i <= r)
#define F(i, L, R) for (int i = L; i < R; i++) // next four are for "for loops"
#define FE(i, L, R) for (int i = L; i <= R; i++)
#define FF(i, L, R) for (int i = L; i > R; i--)
#define FFE(i, L, R) for (int i = L; i >= R; i--)
#define getI(a)                                                                \
  scanf("%d", &a) // next three are handy ways to get ints, it's also force you
                  // to use '&' sign
#define getII(a, b) scanf("%d%d", &a, &b)
#define getIII(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define wez(n)                                                                 \
  int(n);                                                                      \
  scanf(                                                                       \
      "%d",                                                                    \
      &(n)) // handy if the input is right after the definition of a variable
#define wez2(n, m)                                                             \
  int(n), (m);                                                                 \
  scanf("%d %d", &(n), &(m))
#define wez3(n, m, k)                                                          \
  int(n), (m), (k);                                                            \
  scanf("%d %d %d", &(n), &(m), &(k))
#define TESTS wez(testow) while (testow--) // for multilple cases problems
#define whileZ                                                                 \
  int T;                                                                       \
  getI(T);                                                                     \
  while (T--)                             // the same as above
#define getS(x) scanf("%s", x)            // get a char* string
#define clr(a, x) memset(a, x, sizeof(a)) // set array to some value
#define char2Int(c) (c - '0')
#define lastEle(vec) vec[vec.size() - 1]
#define SZ(x) ((int)((x).size()))
#define REMAX(a, b) (a) = max((a), (b)) // set a to the maximum of a and b
#define REMIN(a, b) (a) = min((a), (b));
#define FOREACH(i, t)                                                          \
  for (typeof(t.begin()) i = t.begin(); i != t.end();                          \
       i++)                           // traverse an STL data structure
#define ALL(c) (c).begin(), (c).end() // handy for function like "sort()"
#define PRESENT(c, x) ((c).find(x) != (c).end())
#define CPRESENT(c, x) (find(ALL(c), x) != (c).end())
#define ll                                                                     \
  long long // data types used often, but you don't want to type them time by
            // time
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define IOS                                                                    \
  ios_base::sync_with_stdio(0); // to synchronize the input of cin and scanf
#define INF 1001001001
#define PI 3.1415926535897932384626
// for map, pair
#define mp make_pair
#define fi first
#define se second
// for debug
inline void pisz(int n) { printf("%d\n", n); }
#define DBG(vari) cerr << #vari << " = " << (vari) << endl;
#define printA(a, L, R) FE(i, L, R) cout << a[i] << (i == R ? '\n' : ' ')
#define printV(a) printA(a, 0, a.size() - 1)
#define MAXN 10000
// for vectors
#define pb push_back
typedef int elem_t;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
// directions
const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0},  {-1, 0},
                       {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
template <typename T, typename TT>
ostream &operator<<(ostream &s, pair<T, TT> t) {
  return s << "(" << t.first << "," << t.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, vector<T> t) {
  F(i, 0, SZ(t)) s << t[i] << " ";
  return s;
}

ll mod = 1000000007;

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

ll power(ll a, ll b, ll m) {
  ll r = 1;
  a = a % m;
  while (b) {
    if (b & 1)
      r = r * a % m;
    a = a * a % m;
    b = b >> 1;
  }
  return r;
}
ll findlcm(int arr[], int n) {
  // Initialize result
  ll ans = arr[0];

  // ans contains LCM of arr[0], ..arr[i]
  // after i'th iteration,
  for (int i = 1; i < n; i++)
    ans = (((arr[i] * ans)) / (gcd(arr[i], ans)));

  return ans;
}

ll count = 0;
ll dp[(1 << 21)][22];

ll recur(vector<std::vector<int>> &matrix, int mask, int index) {
  // cout << matrix << "::\n";
  if (mask == (1 << (int)(matrix.size())) - 1 && index == (int)matrix.size()) {
    // cout << "aya\n";
    return 1;
  }
  if (dp[mask][index] != -1)
    return dp[mask][index];
  if (index == (int)matrix.size())
    return 0;

  dp[mask][index] = 0;
  for (int i = 0; i < matrix.size(); ++i) { // bool test = mask & (1 << i);

    // cout << test << "::\n";
    if (((mask & (1 << i)) == 0) && matrix[index][i] == 1) {
      // cout << "tata: " << mask << " toto: " << matrix[index][i] << "\n";
      int temp = mask | (1 << i);
      dp[mask][index] =
          (dp[mask][index] + recur(matrix, temp, index + 1)) % mod;
    }
    /* code */
  }

  return dp[mask][index];
}

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n;
  cin >> n;
  std::vector<std::vector<int>> matrix;
  for (int i = 0; i < n; ++i) {
    std::vector<int> temp;
    for (int j = 0; j < n; ++j) {
      int val;
      cin >> val;
      temp.push_back(val);
      /* code */
    }
    matrix.push_back(temp);
    /* code */
  }
  // cout << matrix << "\n";
  int mask = 0;
  memset(dp, -1, sizeof(dp));
  recur(matrix, mask, 0);

  cout << dp[0][0] << "\n";
}