Problem p02807 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

////////////
// ModInt //
////////////

// 四則演算の最も左に存在する値がModIntでなければキャストでバグる
// 例えばx = mint * 1000;やx = ModInt(1000) * mint;はいいがx = 1000 *
// mint;は駄目。
template <int64_t mod_ = 1'000'000'007> class ModInt {
private:
  int64_t integer_;

public:
  constexpr ModInt(const int64_t initial_number = 0)
      : integer_(initial_number) {}

  // 四則演算
  constexpr ModInt operator+(const ModInt &operand) const {
    ModInt ret{this->integer_ + operand.integer_};
    if (ret.integer_ >= mod_)
      ret.integer_ -= mod_;
    return ret;
  }
  constexpr ModInt operator-(const ModInt &operand) const {
    ModInt ret{this->integer_ - operand.integer_};
    if (ret.integer_ < 0)
      ret.integer_ += mod_;
    return ret;
  }
  constexpr ModInt operator*(const ModInt &operand) const {
    return {this->integer_ * operand.integer_ % mod_};
  }
  constexpr ModInt operator/(const ModInt &operand) const {
    return *this * (operand ^ (mod_ - 2));
  }

  // 単項演算子
  constexpr ModInt &operator++() {
    if (integer_ + 1 == mod_)
      integer_ = 0;
    else
      integer_++;
    return *this;
  }

  constexpr ModInt &operator--() {
    if (integer_ == 0)
      integer_ = mod_ - 1;
    else
      integer_--;
    return *this;
  }

  constexpr ModInt operator+() { return *this; }

  constexpr ModInt operator-() {
    if (integer_ == 0)
      return ModInt(0ll);
    else
      return ModInt(mod_ - integer_);
  }

  // 累乗
  constexpr ModInt operator^(const int64_t operand) const {
    ModInt ret{1}, pow_ope{this->integer_};
    for (int64_t pow{operand}; pow > 0; pow >>= 1) {
      if (pow & 1)
        ret *= pow_ope;
      pow_ope *= pow_ope;
    }
    return ret;
  }

  // 代入
  constexpr ModInt &operator=(const ModInt &operand) {
    this->integer_ = operand.integer_;
    return *this;
  }
  constexpr ModInt &operator+=(const ModInt &operand) {
    *this = *this + operand;
    return *this;
  }
  constexpr ModInt &operator-=(const ModInt &operand) {
    *this = *this - operand;
    return *this;
  }
  constexpr ModInt &operator*=(const ModInt &operand) {
    *this = *this * operand;
    return *this;
  }
  constexpr ModInt &operator/=(const ModInt &operand) {
    *this = *this / operand;
    return *this;
  }

  // その他
  constexpr operator int64_t() { return integer_; }

  constexpr ModInt getOne() const { return ModInt(1ll); }
  constexpr ModInt getZero() const { return ModInt(0ll); }

  static constexpr int64_t mod{mod_};
};
using Mint = ModInt<>;

////////////////
// 組み合わせ //
///////////////

template <typename T = Mint, int max_ = 200'000> class Combination {
public:
  std::array<Mint, max_ + 1> inv, fact, finv;

  constexpr Combination() {
    inv[0] = inv[1] = fact[0] = fact[1] = finv[0] = finv[1] = 1;
    for (int num{2}; num <= max_; num++) {
      inv[num] =
          (T::mod - (int64_t)inv[T::mod % num] * (T::mod / num) % T::mod) %
          T::mod;
      fact[num] = num * fact[num - 1] % T::mod;
      finv[num] = inv[num] * finv[num - 1] % T::mod;
    }
  }

  constexpr Mint getCombi(const int n, const int r) const {
    if (r < 0 || n < 0 || n - r < 0)
      return 0;
    return fact[n] * finv[r] * finv[n - r];
  }

  constexpr Mint getPerm(const int n, const int r) const {
    if (r < 0 || n < 0 || n - r < 0)
      return 0;
    return fact[n] * finv[n - r];
  }
};
Combination<> combi;

int main() {
  int N;
  scanf("%d", &N);
  std::vector<int64_t> x(N);
  for (auto &e : x)
    scanf("%lld", &e);
  std::vector<int64_t> diff(N - 1);
  for (int i{}; i < N - 1; i++)
    diff[i] = x[i + 1] - x[i];
  Mint ans{};
  for (int from{}; from < N - 1; from++) {
    for (int to{from + 1}; to < N - 1; to++)
      ans += (Mint(x[to]) - Mint(x[from])) *
             combi.getCombi(N - 1, to - from + 1) * combi.fact[to - from - 1] *
             combi.fact[N - 1 - (to - from + 1)];
    ans += (Mint(x[N - 1]) - Mint(x[from])) *
           combi.getCombi(N - 1, N - 1 - from) * combi.fact[N - 1 - from - 1] *
           combi.fact[N - 1 - (N - 1 - from)];
  }
  std::cout << ans << std::endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

////////////
// ModInt //
////////////

// 四則演算の最も左に存在する値がModIntでなければキャストでバグる
// 例えばx = mint * 1000;やx = ModInt(1000) * mint;はいいがx = 1000 *
// mint;は駄目。
template <int64_t mod_ = 1'000'000'007> class ModInt {
private:
  int64_t integer_;

public:
  constexpr ModInt(const int64_t initial_number = 0)
      : integer_(initial_number) {}

  // 四則演算
  constexpr ModInt operator+(const ModInt &operand) const {
    ModInt ret{this->integer_ + operand.integer_};
    if (ret.integer_ >= mod_)
      ret.integer_ -= mod_;
    return ret;
  }
  constexpr ModInt operator-(const ModInt &operand) const {
    ModInt ret{this->integer_ - operand.integer_};
    if (ret.integer_ < 0)
      ret.integer_ += mod_;
    return ret;
  }
  constexpr ModInt operator*(const ModInt &operand) const {
    return {this->integer_ * operand.integer_ % mod_};
  }
  constexpr ModInt operator/(const ModInt &operand) const {
    return *this * (operand ^ (mod_ - 2));
  }

  // 単項演算子
  constexpr ModInt &operator++() {
    if (integer_ + 1 == mod_)
      integer_ = 0;
    else
      integer_++;
    return *this;
  }

  constexpr ModInt &operator--() {
    if (integer_ == 0)
      integer_ = mod_ - 1;
    else
      integer_--;
    return *this;
  }

  constexpr ModInt operator+() { return *this; }

  constexpr ModInt operator-() {
    if (integer_ == 0)
      return ModInt(0ll);
    else
      return ModInt(mod_ - integer_);
  }

  // 累乗
  constexpr ModInt operator^(const int64_t operand) const {
    ModInt ret{1}, pow_ope{this->integer_};
    for (int64_t pow{operand}; pow > 0; pow >>= 1) {
      if (pow & 1)
        ret *= pow_ope;
      pow_ope *= pow_ope;
    }
    return ret;
  }

  // 代入
  constexpr ModInt &operator=(const ModInt &operand) {
    this->integer_ = operand.integer_;
    return *this;
  }
  constexpr ModInt &operator+=(const ModInt &operand) {
    *this = *this + operand;
    return *this;
  }
  constexpr ModInt &operator-=(const ModInt &operand) {
    *this = *this - operand;
    return *this;
  }
  constexpr ModInt &operator*=(const ModInt &operand) {
    *this = *this * operand;
    return *this;
  }
  constexpr ModInt &operator/=(const ModInt &operand) {
    *this = *this / operand;
    return *this;
  }

  // その他
  constexpr operator int64_t() { return integer_; }

  constexpr ModInt getOne() const { return ModInt(1ll); }
  constexpr ModInt getZero() const { return ModInt(0ll); }

  static constexpr int64_t mod{mod_};
};
using Mint = ModInt<>;

////////////////
// 組み合わせ //
///////////////

template <typename T = Mint, int max_ = 200'000> class Combination {
public:
  std::array<Mint, max_ + 1> inv, fact, finv;

  constexpr Combination() {
    inv[0] = inv[1] = fact[0] = fact[1] = finv[0] = finv[1] = 1;
    for (int num{2}; num <= max_; num++) {
      inv[num] =
          (T::mod - (int64_t)inv[T::mod % num] * (T::mod / num) % T::mod) %
          T::mod;
      fact[num] = num * fact[num - 1] % T::mod;
      finv[num] = inv[num] * finv[num - 1] % T::mod;
    }
  }

  constexpr Mint getCombi(const int n, const int r) const {
    if (r < 0 || n < 0 || n - r < 0)
      return 0;
    return fact[n] * finv[r] * finv[n - r];
  }

  constexpr Mint getPerm(const int n, const int r) const {
    if (r < 0 || n < 0 || n - r < 0)
      return 0;
    return fact[n] * finv[n - r];
  }
};
Combination<> combi;

int main() {
  int N;
  scanf("%d", &N);
  std::vector<int64_t> x(N);
  for (auto &e : x)
    scanf("%lld", &e);
  std::vector<int64_t> diff(N - 1);
  for (int i{}; i < N - 1; i++)
    diff[i] = x[i + 1] - x[i];
  Mint ans{}, inv_sum{};
  for (int i{}; i < N - 1; i++) {
    inv_sum += combi.inv[i + 1];
    ans += combi.fact[N - 1] * Mint(diff[i]) * inv_sum;
  }
  std::cout << ans << std::endl;

  return 0;
}