Problem p03406 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}

const int MAX = 70000;

typedef long long ll;
const ll MOD = 1000000007;

ll fac[MAX], finv[MAX], inv[MAX];
inline long long mod(long long a, long long m) { return (a % m + m) % m; }
long long pow(long long a, long long n, long long m) {
  if (n == 0)
    return 1 % m;
  long long t = pow(a, n / 2, m);
  t = mod(t * t, m);
  if (n & 1)
    t = mod(t * a, m);
  return t;
}

long long Inv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  return mod(u, m);
}

ll GCD(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
ll LCM(ll a, ll b) {
  ll g = GCD(a, b);
  return a / g * b;
}
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int N, M;
int A[21];

long long pre[21][21];
long long dp[21][(1 << 16) + 10];

long long solve() {
  /*
  for (int i = 0; i < M; ++i) {
      for (int k = 1; k <= N; ++k) {
          long long bunshi = COM((int)(1<<N) - A[i], (int)(1<<(k-1)) - 1);
          long long bunbo = COM((int)(1<<N) - 1, (int)(1<<(k-1)));
          pre[i][k-1] = bunshi * Inv(bunbo, MOD) % MOD;

          cout << i << "(" << A[i] << "), " << k << ": " << pre[i][k-1] *
  fac[1<<N] % MOD << endl;
      }
  }
   */

  memset(dp, 0, sizeof(dp));
  long long res = fac[1 << N];

  dp[0][0] = 1;
  for (int i = 0; i < M; ++i) {
    for (int bit = 0; bit < (1 << N); ++bit) {
      dp[i + 1][bit] += dp[i][bit];
      dp[i + 1][bit] %= MOD;

      int rem = 0;
      for (int j = 0; j < N; ++j) {
        if (bit & (1 << j)) {
          rem += (1 << j);
        }
      }
      int NN = (int)(1 << N) - rem;

      for (int k = 0; k < N; ++k) {
        if (bit & (1 << k))
          continue;
        int nbit = bit | (1 << k);

        long long add = 0;
        if (NN >= A[i]) {
          long long bunshi = COM(NN - A[i], (int)(1 << (k)) - 1);
          long long bunbo = COM(NN - 1, (int)(1 << (k)));
          if (bunbo > 0) {
            add = bunshi * Inv(bunbo, MOD) % MOD;
          }
        }

        dp[i + 1][nbit] += (dp[i][bit] * add % MOD);
        dp[i + 1][nbit] %= MOD;
      }
    }
  }

  /*
  for (int i = 0; i <= M; ++i) {
      for (int bit = 0; bit < (1<<N); ++bit) {
          cout << i << ", " << bitset<2>(bit) << ": " << dp[i][bit] << endl;
      }
  }
   */

  for (int bit = 1; bit < (1 << N); ++bit) {
    int con = 0;
    for (int k = 0; k < N; ++k)
      if (bit & (1 << k))
        ++con;
    if (con & 1) {
      res -= dp[M][bit] * fac[1 << N] % MOD;
    } else {
      res += dp[M][bit] * fac[1 << N] % MOD;
    }
    res = mod(res, MOD);
  }

  return res;
}

int main() {
  COMinit();
  while (cin >> N >> M) {
    for (int i = 0; i < M; ++i)
      cin >> A[i];
    sort(A, A + M, greater<int>());
    cout << solve() << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}

const int MAX = 70000;

typedef long long ll;
const ll MOD = 1000000007;

ll fac[MAX], finv[MAX], inv[MAX];
inline long long mod(long long a, long long m) { return (a % m + m) % m; }
long long pow(long long a, long long n, long long m) {
  if (n == 0)
    return 1 % m;
  long long t = pow(a, n / 2, m);
  t = mod(t * t, m);
  if (n & 1)
    t = mod(t * a, m);
  return t;
}

long long Inv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  return mod(u, m);
}

ll GCD(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
ll LCM(ll a, ll b) {
  ll g = GCD(a, b);
  return a / g * b;
}
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int N, M;
int A[21];

long long pre[21][21];
long long dp[21][(1 << 16) + 10];

long long solve() {
  /*
  for (int i = 0; i < M; ++i) {
      for (int k = 1; k <= N; ++k) {
          long long bunshi = COM((int)(1<<N) - A[i], (int)(1<<(k-1)) - 1);
          long long bunbo = COM((int)(1<<N) - 1, (int)(1<<(k-1)));
          pre[i][k-1] = bunshi * Inv(bunbo, MOD) % MOD;

          cout << i << "(" << A[i] << "), " << k << ": " << pre[i][k-1] *
  fac[1<<N] % MOD << endl;
      }
  }
   */

  memset(dp, 0, sizeof(dp));
  long long res = fac[1 << N];

  dp[0][0] = 1;
  for (int i = 0; i < M; ++i) {
    for (int bit = 0; bit < (1 << N); ++bit) {
      dp[i + 1][bit] += dp[i][bit];
      dp[i + 1][bit] %= MOD;

      int rem = 0;
      for (int j = 0; j < N; ++j) {
        if (bit & (1 << j)) {
          rem += (1 << j);
        }
      }
      int NN = (int)(1 << N) - rem;

      for (int k = 0; k < N; ++k) {
        if (bit & (1 << k))
          continue;
        int nbit = bit | (1 << k);

        long long add = 1;

        if (NN - A[i] < 0)
          continue;
        else
          add = add * fac[NN - A[i]] % MOD;

        if (NN - 1 < 0)
          continue;
        else
          add = add * finv[NN - 1] % MOD;

        add = add * finv[(1 << k) - 1] % MOD;
        add = add * fac[(1 << k)] % MOD;

        if (NN - A[i] - (1 << k) + 1 < 0)
          continue;
        else
          add = add * finv[NN - A[i] - (1 << k) + 1] % MOD;

        if (NN - (1 << k) - 1 < 0)
          continue;
        else
          add = add * fac[NN - (1 << k) - 1] % MOD;

        dp[i + 1][nbit] += (dp[i][bit] * add % MOD);
        dp[i + 1][nbit] %= MOD;
      }
    }
  }

  /*
  for (int i = 0; i <= M; ++i) {
      for (int bit = 0; bit < (1<<N); ++bit) {
          cout << i << ", " << bitset<2>(bit) << ": " << dp[i][bit] << endl;
      }
  }
   */

  for (int bit = 1; bit < (1 << N); ++bit) {
    int con = 0;
    for (int k = 0; k < N; ++k)
      if (bit & (1 << k))
        ++con;
    if (con & 1) {
      res -= dp[M][bit] * fac[1 << N] % MOD;
    } else {
      res += dp[M][bit] * fac[1 << N] % MOD;
    }
    res = mod(res, MOD);
  }

  return res;
}

int main() {
  COMinit();
  while (cin >> N >> M) {
    for (int i = 0; i < M; ++i)
      cin >> A[i];
    sort(A, A + M, greater<int>());
    cout << solve() << endl;
  }
}