Problem p03082 (C++)

Buggy submission (status: Time Limit Exceeded):
// 見せてやるぜ、奇跡ってやつをよぉ……
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define DEBUG_VLL(vec)                                                         \
  REP(sz, vec.size()) cerr << vec[sz] << (sz == vec.size() - 1 ? '\n' : ' ');

const ll MOD = 1000000007;
const ll HIGHINF = (ll)1e18;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, x;
  cin >> n >> x;
  vll s(n);
  REP(i, n) cin >> s[i];
  sort(ALL(s));
  vvll dp(n + 1, vll(x + 1, 0));
  REP(i, x + 1) dp[0][i] = i;
  REP(i, n) {
    REP(j, x + 1) {
      // 順番の最初に入れる場合
      dp[i + 1][j] += dp[i][j % s[i]];
      if (dp[i + 1][j] >= MOD)
        dp[i + 1][j] -= MOD;
      // 最初以降（つまり、余りに関係ない場合）
      dp[i + 1][j] += dp[i][j] * i % MOD;
      if (dp[i + 1][j] >= MOD)
        dp[i + 1][j] -= MOD;
    }
    REP(j, x + 1) DEBUG_VLL(dp[i + 1]) cout << '\n';
  }

  cout << dp[n][x] << '\n';
  return 0;
}


Fixed submission:
// 見せてやるぜ、奇跡ってやつをよぉ……
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define DEBUG_VLL(vec)                                                         \
  REP(sz, vec.size()) cerr << vec[sz] << (sz == vec.size() - 1 ? '\n' : ' ');

const ll MOD = 1000000007;
const ll HIGHINF = (ll)1e18;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, x;
  cin >> n >> x;
  vll s(n);
  REP(i, n) cin >> s[i];
  sort(ALL(s));
  vvll dp(n + 1, vll(x + 1, 0));
  REP(i, x + 1) dp[0][i] = i;
  REP(i, n) {
    REP(j, x + 1) {
      // 順番の最初に入れる場合
      dp[i + 1][j] += dp[i][j % s[i]];
      if (dp[i + 1][j] >= MOD)
        dp[i + 1][j] -= MOD;
      // 最初以降（つまり、余りに関係ない場合）
      dp[i + 1][j] += dp[i][j] * i % MOD;
      if (dp[i + 1][j] >= MOD)
        dp[i + 1][j] -= MOD;
    }
    cout << '\n';
  }

  cout << dp[n][x] << '\n';
  return 0;
}