Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using qll = queue<ll>;
using vb = vector<bool>;
using mll = map<ll, ll>;
using sll = stack<ll>;
#define REP(i, n) for (ll i(0); (i) < (n); (i)++)
#define rep(i, n) for (ll i(0); (i) < (n); (i)++)
#define ALL(a) a.begin(), a.end()
#define enld endl //* missspell check
const ll INF = 1LL << 60;

const long long MOD = 1000000007;

ll cntOne(ll msk) {
  ll res = 0;
  REP(i, 64) { res += (msk >> i) & 1; }
  return res;
}

void solve(long long N, std::vector<std::vector<long long>> a) {
  vector<vll> dp(N + 1, vll(1LL << N, 0));
  dp[0][0] = 1;
  REP(i, N) {
    REP(msk, 1LL << N) {
      if (cntOne(msk) == i + 1) {
        REP(j, N) {
          if (a[i][j] && ((msk >> j) & 1)) {
            dp[i + 1][msk] += dp[i][msk & (~(1LL << j))];
          }
          dp[i + 1][msk] %= MOD;
        }
      }
    }
  }
  cout << dp[N][(1LL << N) - 1] << endl;
  return;
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<std::vector<long long>> a(N, std::vector<long long>(N));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      scanf("%lld", &a[i][j]);
    }
  }
  solve(N, std::move(a));
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using qll = queue<ll>;
using vb = vector<bool>;
using mll = map<ll, ll>;
using sll = stack<ll>;
#define REP(i, n) for (ll i(0); (i) < (n); (i)++)
#define rep(i, n) for (ll i(0); (i) < (n); (i)++)
#define ALL(a) a.begin(), a.end()
#define enld endl //* missspell check
const ll INF = 1LL << 60;

const long long MOD = 1000000007;

ll cntOne(ll msk) {
  ll res = 0;
  REP(i, 64) { res += (msk >> i) & 1; }
  return res;
}

void solve(long long N, std::vector<std::vector<long long>> a) {
  vector<vll> dp(N + 1, vll(1LL << N, 0));
  dp[0][0] = 1;
  REP(i, N) {
    REP(msk, 1LL << N) {
      if (__builtin_popcount(msk) == i + 1) {
        REP(j, N) {
          if (a[i][j] && ((msk >> j) & 1)) {
            dp[i + 1][msk] += dp[i][msk & (~(1LL << j))];
          }
          dp[i + 1][msk] %= MOD;
        }
      }
    }
  }
  cout << dp[N][(1LL << N) - 1] << endl;
  return;
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<std::vector<long long>> a(N, std::vector<long long>(N));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      scanf("%lld", &a[i][j]);
    }
  }
  solve(N, std::move(a));
  return 0;
}