Problem p02810 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,ssse3,tune=native")
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pi = pair<ll, ll>;
using vpi = vector<pi>;
const int maxn = 113, maxl = 503, mod = 1e9 + 7;
int n, x, dp[maxn][maxn][2 * maxl], sm[maxn][maxn][2 * maxl],
    ssm[maxn][maxn][2 * maxl];
int fact[maxn], ifact[maxn];
vi l;
int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
int sub(int x, int y) {
  if (y)
    y = mod - y;
  return add(x, y);
}
int mul(int x, int y) { return x * 1ll * y % mod; }
int setup() {
  fact[0] = ifact[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fact[i] = mul(fact[i - 1], i);
    if (i == 1)
      ifact[i] = 1;
    else
      ifact[i] = (mod - mul(ifact[mod % i], mod / i));
  }
  for (int i = 2; i < maxn; i++)
    ifact[i] = mul(ifact[i], ifact[i - 1]);
}
int nck(int n, int k) {
  if (k > n)
    return 0;
  return mul(fact[n], mul(ifact[k], ifact[n - k]));
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  setup();
  cin >> n >> x;
  l.resize(n);
  for (auto &i : l)
    cin >> i;
  sort(rall(l));
  dp[0][0][0] = 1;
  int &X = x;
  for (int i = 0; i <= n; i++) {
    for (int k = 0; k <= n; k++) {
      int running = 0;
      for (int s = 0; s <= x; s++) {
        running = add(running, sm[i][k][s]);
        dp[i][k][s] = add(dp[i][k][s], running);
        if (i == n)
          continue;
        dp[i + 1][k + 1][s + l[i]] =
            add(dp[i + 1][k + 1][s + l[i]], dp[i][k][s]);
        int t;
        if (k > 1) {
          t = mul(mul(k, (k - 1)), dp[i][k][s]);
          for (int x = 0; x < l[i] - 1 && s + x <= X; x++) {
            dp[i + 1][k - 1][s + x] =
                add(dp[i + 1][k - 1][s + x], mul(t, l[i] - x - 1));
          }
        }
        if (k) {
          t = mul(k, dp[i][k][s]);
          t = mul(t, 2);
          sm[i + 1][k][s + 1] = add(sm[i + 1][k][s + 1], t);
          sm[i + 1][k][s + l[i]] = sub(sm[i + 1][k][s + l[i]], t);
          dp[i + 1][k][s] =
              add(dp[i + 1][k][s], mul(dp[i][k][s], sub(s, mul(k, l[i] - 1))));
        }
      }
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; i++)
    ans = add(ans, mul(dp[n][i][x], fact[i]));
  cout << ans << '\n';
}


Fixed submission:
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,ssse3,tune=native")
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pi = pair<ll, ll>;
using vpi = vector<pi>;
const int maxn = 113, maxl = 503, mod = 1e9 + 7;
int n, x, dp[maxn][maxn][2 * maxl], sm[maxn][maxn][2 * maxl],
    ssm[maxn][maxn][2 * maxl];
int fact[maxn], ifact[maxn];
vi l;
int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
int sub(int x, int y) {
  if (y)
    y = mod - y;
  return add(x, y);
}
int mul(int x, int y) { return x * 1ll * y % mod; }
int setup() {
  fact[0] = ifact[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fact[i] = mul(fact[i - 1], i);
    if (i == 1)
      ifact[i] = 1;
    else
      ifact[i] = (mod - mul(ifact[mod % i], mod / i));
  }
  for (int i = 2; i < maxn; i++)
    ifact[i] = mul(ifact[i], ifact[i - 1]);
}
int nck(int n, int k) {
  if (k > n)
    return 0;
  return mul(fact[n], mul(ifact[k], ifact[n - k]));
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  setup();
  cin >> n >> x;
  l.resize(n);
  for (auto &i : l)
    cin >> i;
  sort(rall(l));
  dp[0][0][0] = 1;
  int &X = x;
  for (int i = 0; i <= n; i++) {
    for (int k = 0; k <= n; k++) {
      int running = 0;
      for (int s = 0; s <= x; s++) {
        running = add(running, sm[i][k][s]);
        dp[i][k][s] = add(dp[i][k][s], running);
        if (!dp[i][k][s])
          continue;
        if (i == n)
          continue;
        dp[i + 1][k + 1][s + l[i]] =
            add(dp[i + 1][k + 1][s + l[i]], dp[i][k][s]);
        int t;
        if (k > 1) {
          t = mul(mul(k, (k - 1)), dp[i][k][s]);
          for (int x = 0; x < l[i] - 1 && s + x <= X; x++) {
            dp[i + 1][k - 1][s + x] =
                add(dp[i + 1][k - 1][s + x], mul(t, l[i] - x - 1));
          }
        }
        if (k) {
          t = mul(k, dp[i][k][s]);
          t = mul(t, 2);
          sm[i + 1][k][s + 1] = add(sm[i + 1][k][s + 1], t);
          sm[i + 1][k][s + l[i]] = sub(sm[i + 1][k][s + l[i]], t);
          dp[i + 1][k][s] =
              add(dp[i + 1][k][s], mul(dp[i][k][s], sub(s, mul(k, l[i] - 1))));
        }
      }
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; i++)
    ans = add(ans, mul(dp[n][i][x], fact[i]));
  cout << ans << '\n';
}