Problem p03406 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;
const int MAXN = 17;
const int MAXM = 1 << MAXN;
const int MOD = 1e9 + 7;

int n, m, ans;
int a[MAXN], f[MAXN][MAXM], fac[MAXM], ifac[MAXM];

int power(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = 1ll * res * x % MOD;
    x = 1ll * x * x % MOD;
    y >>= 1;
  }
  return res;
}

void Init(int n) {
  fac[0] = ifac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = 1ll * fac[i - 1] * i % MOD;
  ifac[n] = power(fac[n], MOD - 2);
  for (int i = n - 1; i >= 1; i--)
    ifac[i] = 1ll * ifac[i + 1] * (i + 1) % MOD;
}

int C(int n, int m) {
  if (n < m || m < 0)
    return 0;
  return 1ll * fac[n] * ifac[n - m] % MOD * ifac[m] % MOD;
}

int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++)
    scanf("%d", &a[i]);
  int maxState = (1 << n);
  Init(maxState);
  f[m + 1][0] = 1;
  for (int i = m; i >= 1; i--) {
    for (int S = 0; S < maxState; S++) {
      f[i][S] = (f[i][S] + f[i + 1][S]) % MOD;
      for (int j = 0; j < n; j++) {
        if (S & (1 << j))
          continue;
        f[i][S | (1 << j)] =
            (f[i][S | (1 << j)] + 1ll * f[i + 1][S] *
                                      C(maxState - S - a[i], (1 << j) - 1) %
                                      MOD * fac[1 << j] % MOD) %
            MOD;
      }
    }
  }
  for (int S = 0; S < maxState; S++) {
    int cnt = 0;
    for (int i = S; i; i -= (i & -i))
      cnt++;
    if (cnt & 1)
      ans = (ans - 1ll * f[1][S] * fac[maxState - S - 1] % MOD + MOD) % MOD;
    else
      ans = (ans + 1ll * f[1][S] * fac[maxState - S - 1] % MOD) % MOD;
  }
  ans = 1ll * ans * maxState % MOD;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;
const int MAXN = 18;
const int MAXM = 1 << MAXN;
const int MOD = 1e9 + 7;

int n, m, ans;
int a[MAXN], f[MAXN][MAXM], fac[MAXM], ifac[MAXM];

int power(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = 1ll * res * x % MOD;
    x = 1ll * x * x % MOD;
    y >>= 1;
  }
  return res;
}

void Init(int n) {
  fac[0] = ifac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = 1ll * fac[i - 1] * i % MOD;
  ifac[n] = power(fac[n], MOD - 2);
  for (int i = n - 1; i >= 1; i--)
    ifac[i] = 1ll * ifac[i + 1] * (i + 1) % MOD;
}

int C(int n, int m) {
  if (n < m || m < 0)
    return 0;
  return 1ll * fac[n] * ifac[n - m] % MOD * ifac[m] % MOD;
}

int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++)
    scanf("%d", &a[i]);
  int maxState = (1 << n);
  Init(maxState);
  f[m + 1][0] = 1;
  for (int i = m; i >= 1; i--) {
    for (int S = 0; S < maxState; S++) {
      f[i][S] = (f[i][S] + f[i + 1][S]) % MOD;
      for (int j = 0; j < n; j++) {
        if (S & (1 << j))
          continue;
        f[i][S | (1 << j)] =
            (f[i][S | (1 << j)] + 1ll * f[i + 1][S] *
                                      C(maxState - S - a[i], (1 << j) - 1) %
                                      MOD * fac[1 << j] % MOD) %
            MOD;
      }
    }
  }
  for (int S = 0; S < maxState; S++) {
    int cnt = 0;
    for (int i = S; i; i -= (i & -i))
      cnt++;
    if (cnt & 1)
      ans = (ans - 1ll * f[1][S] * fac[maxState - S - 1] % MOD + MOD) % MOD;
    else
      ans = (ans + 1ll * f[1][S] * fac[maxState - S - 1] % MOD) % MOD;
  }
  ans = 1ll * ans * maxState % MOD;
  printf("%d\n", ans);
  return 0;
}