Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a) for (int i = 0; i < (a); i++)
#define ALL(a) (a).begin(), (a).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;

using ll = long long;
template <ll MOD = 1000000007> struct Mint {
  ll x;

  Mint() : x(0) {}
  Mint(ll t) {
    x = t % MOD;
    if (x < 0)
      x += MOD;
  }

  Mint pow(ll n) {
    Mint res(1), t(x);
    while (n > 0) {
      if (n & 1)
        res *= t;
      t *= t;
      n >>= 1;
    }
    return res;
  }

  Mint inv() const {
    ll a = x, b = MOD, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    return Mint(u);
  }

  Mint &operator+=(Mint a) {
    x += a.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }
  Mint &operator-=(Mint a) {
    x += MOD - a.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }
  Mint &operator*=(Mint a) {
    x = 1LL * x * a.x % MOD;
    return *this;
  }
  Mint &operator/=(Mint a) { return (*this) *= a.inv(); }

  Mint operator+(Mint a) const { return Mint(x) += a; }
  Mint operator-(Mint a) const { return Mint(x) -= a; }
  Mint operator*(Mint a) const { return Mint(x) *= a; }
  Mint operator/(Mint a) const { return Mint(x) /= a; }

  Mint operator-() const { return Mint(-x); }

  bool operator==(const Mint a) { return x == a.x; }
  bool operator!=(const Mint a) { return x != a.x; }
  bool operator<(const Mint a) { return x < a.x; }

  friend ostream &operator<<(ostream &os, const Mint &a) { return os << a.x; }

  friend istream &operator>>(istream &is, Mint &a) {
    ll t;
    is >> t;
    a = Mint<MOD>(t);
    return (is);
  }
};

signed main() {
  int n;
  cin >> n;
  vector<int> vec[n];
  REP(i, n) {
    REP(j, n) {
      int a;
      cin >> a;
      if (a == 1) {
        vec[i].emplace_back(1 << j);
      }
    }
  }
  vector<vector<ll>> dp(vector<vector<ll>>(n + 1, vector<ll>(1 << n, 0)));
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < vec[i].size(); j++) {
      for (int k = 0; k < (1 << n); k++) {
        dp[i + 1][k | vec[i][j]] += dp[i][k];
        dp[i + 1][k | vec[i][j]] %= MOD;
      }
    }
  }
  cout << dp[n][(1 << n) - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a) for (int i = 0; i < (a); i++)
#define ALL(a) (a).begin(), (a).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;

using ll = long long;
template <ll MOD = 1000000007> struct Mint {
  ll x;

  Mint() : x(0) {}
  Mint(ll t) {
    x = t % MOD;
    if (x < 0)
      x += MOD;
  }

  Mint pow(ll n) {
    Mint res(1), t(x);
    while (n > 0) {
      if (n & 1)
        res *= t;
      t *= t;
      n >>= 1;
    }
    return res;
  }

  Mint inv() const {
    ll a = x, b = MOD, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    return Mint(u);
  }

  Mint &operator+=(Mint a) {
    x += a.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }
  Mint &operator-=(Mint a) {
    x += MOD - a.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }
  Mint &operator*=(Mint a) {
    x = 1LL * x * a.x % MOD;
    return *this;
  }
  Mint &operator/=(Mint a) { return (*this) *= a.inv(); }

  Mint operator+(Mint a) const { return Mint(x) += a; }
  Mint operator-(Mint a) const { return Mint(x) -= a; }
  Mint operator*(Mint a) const { return Mint(x) *= a; }
  Mint operator/(Mint a) const { return Mint(x) /= a; }

  Mint operator-() const { return Mint(-x); }

  bool operator==(const Mint a) { return x == a.x; }
  bool operator!=(const Mint a) { return x != a.x; }
  bool operator<(const Mint a) { return x < a.x; }

  friend ostream &operator<<(ostream &os, const Mint &a) { return os << a.x; }

  friend istream &operator>>(istream &is, Mint &a) {
    ll t;
    is >> t;
    a = Mint<MOD>(t);
    return (is);
  }
};

signed main() {
  int n;
  cin >> n;
  vector<int> vec[n];
  REP(i, n) {
    REP(j, n) {
      int a;
      cin >> a;
      if (a == 1) {
        vec[i].emplace_back(1 << j);
      }
    }
  }
  vector<vector<ll>> dp(vector<vector<ll>>(n + 1, vector<ll>(1 << n, 0)));
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int bit = 0; bit < (1 << n); bit++) {
      if (i != __builtin_popcount(bit))
        continue;
      for (int j = 0; j < vec[i].size(); j++) {
        dp[i + 1][bit | vec[i][j]] += dp[i][bit];
        dp[i + 1][bit | vec[i][j]] %= MOD;
      }
    }
  }
  cout << dp[n][(1 << n) - 1] << endl;
  return 0;
}