Language: C++


#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>
#include <array>

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { ++i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_): l(l_), r(std::max(l_, r_)) { }
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { --i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr revrange(int l_, int r_): l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

template <uint32_t Modulus>
class modular {
public:
  using value_type = uint32_t;
  using max_type = uint64_t;

  static constexpr value_type mod = Modulus;
  static constexpr value_type mod_min = 1;
  static constexpr value_type mod_max = 2147483647;
  static_assert(mod >= mod_min, "invalid mod :: too small");
  static_assert(mod <= mod_max, "invalid mod :: too big");

  template <class T>
  static constexpr value_type normalize(T value_) {
    if (value_ < 0) {
      value_ = -value_;
      value_ %= mod;
      if (value_ == 0) return 0;
      return mod - value_;
    }
    return value_ % mod;
  }

private:
  value_type value;

public:
  constexpr modular(): value(0) { }
  template <class T>
  explicit constexpr modular(T value_): value(normalize(value_)) { }
  template <class T>
  explicit constexpr operator T() { return static_cast<T>(value); }

  constexpr value_type operator () () const { return value; }
  constexpr modular operator - () const { return modular(mod - value); }
  constexpr modular operator ~ () const { return inverse(); }

  constexpr modular inverse() const { return power(mod - 2); }
  constexpr modular power(max_type exp) const {
    modular res(1), mult(*this);
    while (exp > 0) {
      if (exp & 1) res *= mult;
      mult *= mult;
      exp >>= 1;
    }
    return res;
  }

  constexpr modular operator + (const modular &rhs) const { return modular(*this) += rhs; }
  constexpr modular& operator += (const modular &rhs) { 
    if ((value += rhs.value) >= mod) value -= mod; 
    return *this; 
  }

  constexpr modular operator - (const modular &rhs) const { return modular(*this) -= rhs; }
  constexpr modular& operator -= (const modular &rhs) { 
    if ((value += mod - rhs.value) >= mod) value -= mod; 
    return *this; 
  }

  constexpr modular operator * (const modular &rhs) const { return modular(*this) *= rhs; }
  constexpr modular& operator *= (const modular &rhs) { 
    value = (max_type) value * rhs.value % mod;
    return *this;
  }

  constexpr modular operator / (const modular &rhs) const { return modular(*this) /= rhs; }
  constexpr modular& operator /= (const modular &rhs) { return (*this) *= rhs.inverse(); }

  constexpr bool zero() const { return value == 0; }
  constexpr bool operator == (const modular &rhs) const { return value == rhs.value; }
  constexpr bool operator != (const modular &rhs) const { return value != rhs.value; }
  friend std::ostream& operator << (std::ostream &stream, const modular &rhs) {
    return stream << rhs.value;
  }

};

template <class T, std::size_t N>
class factorials {
public:
  using value_type = T;
  static constexpr std::size_t size = N;

public:
  std::array<value_type, size + 1> fact{};
  std::array<value_type, size + 1> fact_inv{};

  factorials() {
    fact.front() = value_type(1);
    for (std::size_t i = 1; i <= size; ++i) {
      fact[i] = fact[i - 1] * value_type(i);
    }
    fact_inv.back() = ~fact.back();
    for (std::size_t i = size; i > 0; --i) {
      fact_inv[i - 1] = fact_inv[i] * value_type(i);
    }
  }

  value_type operator () (std::size_t n, std::size_t r) const {
    return fact[n] * fact_inv[n - r] * fact_inv[r];
  }

};

using modint = modular<1000000007>;
factorials<modint, 1000000> fact;

int main() {
  int N;
  std::cin >> N;
  if (N == 2) {
    std::cout << "1\n";
    return 0;
  }
  auto calc = [&](int black, int white) {
    if (black < 0 || white < 0) return modint(0);
    if (white > black + 1) return modint(0);
    return fact(black + 1, white);
  };
  auto expected1 = [&](int black, int white) {
    if (black < 2 || white < 0) return modint(0);
    return modint(black) * modint(black - 1) * fact.fact[black + white - 2] * fact.fact_inv[black + white];
  };
  auto expected2 = [&](int black, int white) {
    if (black < 1 || white < 0) return modint(0);
    return modint(black) * fact.fact[black + white - 1] * fact.fact_inv[black + white];
  };
  modint ans;
  for (int put: range(2, N)) {
    int empty = N - put - 1;
    modint coeff(2 * empty + 2);
    coeff -= modint(put - 2) * expected1(empty, put - empty - 1);
    coeff -= modint(2) * expected2(empty, put - empty - 1);
    ans += coeff * calc(put - 2, empty) * fact.fact[put] * fact.fact[empty];
  }
  std::cout << ans << '\n';
  return 0;
}


Compiler output:
a.cc:57:11: error: 'uint32_t' has not been declared
   57 | template <uint32_t Modulus>
      |           ^~~~~~~~
a.cc:60:22: error: 'uint32_t' does not name a type
   60 |   using value_type = uint32_t;
      |                      ^~~~~~~~
a.cc:8:1: note: 'uint32_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
    7 | #include <array>
  +++ |+#include <cstdint>
    8 | 
a.cc:61:20: error: 'uint64_t' does not name a type
   61 |   using max_type = uint64_t;
      |                    ^~~~~~~~
a.cc:61:20: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
a.cc:63:20: error: 'value_type' does not name a type
   63 |   static constexpr value_type mod = Modulus;
      |                    ^~~~~~~~~~
a.cc:64:20: error: 'value_type' does not name a type
   64 |   static constexpr value_type mod_min = 1;
      |                    ^~~~~~~~~~
a.cc:65:20: error: 'value_type' does not name a type
   65 |   static constexpr value_type mod_max = 2147483647;
      |                    ^~~~~~~~~~
a.cc:66:17: error: 'mod' was not declared in this scope
   66 |   static_assert(mod >= mod_min, "invalid mod :: too small");
      |                 ^~~
a.cc:66:24: error: 'mod_min' was not declared in this scope
   66 |   static_assert(mod >= mod_min, "invalid mod :: too small");
      |                        ^~~~~~~
a.cc:67:17: error: 'mod' was not declared in this scope
   67 |   static_assert(mod <= mod_max, "invalid mod :: too big");
      |                 ^~~
a.cc:67:24: error: 'mod_max' was not declared in this scope
   67 |   static_assert(mod <= mod_max, "invalid mod :: too big");
      |                        ^~~~~~~
a.cc:70:20: error: 'value_type' does not name a type
   70 |   static constexpr value_type normalize(T value_) {
      |                    ^~~~~~~~~~
a.cc:81:3: error: 'value_type' does not name a type
   81 |   value_type value;
      |   ^~~~~~~~~~
a.cc:90:13: error: 'value_type' does not name a type
   90 |   constexpr value_type operator () () const { return value; }
      |             ^~~~~~~~~~
a.cc:95:27: error: 'max_type' has not been declared
   95 |   constexpr modular power(max_type exp) const {
      |                           ^~~~~~~~
a.cc:163:12: error: 'modint' was not declared in this scope; did you mean 'mode_t'?
  163 | factorials<modint, 1000000> fact;
      |            ^~~~~~
      |            mode_t
a.cc:163:27: error: template argument 1 is invalid
  163 | factorials<modint, 1000000> fact;
      |                           ^
a.cc: In lambda function:
a.cc:173:40: error: 'modint' was not declared in this scope; did you mean 'mode_t'?
  173 |     if (black < 0 || white < 0) return modint(0);
      |                                        ^~~~~~
      |                                        mode_t
a.cc:174:35: error: 'modint' was not declared in this scope; did you mean 'mode_t'?
  174 |     if (white > black + 1) return modint(0);
      |                                   ^~~~~~
      |                                   mode_t
a.cc:175:16: error: 'fact' cannot be used as a function
  175 |     return fact(black + 1, white);
      |            ~~~~^~~~~~~~~~~~~~~~~~
a.cc: In lambda function:
a.cc:178:40: error: 'modint' was not declared in this scope; did you mean 'mode_t'?
  178 |     if (black < 2 || white < 0) return modint(0);
      |                                        ^~~~~~
      |                                        mode_t
a.cc:179:12: error: 'modint' was not declared in this scope; did you mean 'mode_t'?
  179 |     return modint(black) * modint(black - 1) * fact.fact[black + white - 2] * fact.fact_inv[black + white];
      |            ^~~~~~
      |            mode_t
a.cc:179:53: error: request for member 'fact' in 'fact', which is of non-class type 'int'
  179 |     return modint(black) * modint(black - 1) * fact.fact[black + white - 2] * fact.fact_inv[black + white];
      |                                                     ^~~~
a.cc:179:84: error: request for member 'fact_inv' in 'fact', which is of non-class type 'int'
  179 |     return modint(black) * modint(black - 1) * fact.fact[black + white - 2] * fact.fact_inv[black + white];
      |                                                                                    ^~~~~~~~
a.cc: In lambda function:
a.cc:182:40: error: 'modint' was not declared in this scope; did you mean 'mode_t'?
  182 |     if (black < 1 || white < 0) return modint(0);
      |                                        ^~~~~~
      |                                        mode_t
a.cc:183:12: error: 'modint' was not declared in this scope; did you mean 'mode_t'?
  183 |     return modint(black) * fact.fact[black + white - 1] * fact.fact_inv[black + white];
      |            ^~~~~~
      |            mode_t
a.cc:183:33: error: request for member 'fact' in 'fact', which is of non-class type 'int'
  183 |     return modint(black) * fact.fact[black + white - 1] * fact.fact_inv[black + white];
      |                                 ^~~~
a.cc:183:64: error: request for member 'fact_inv' in 'fact', which is of non-class type 'int'
  183 |     return modint(black) * fact.fact[black + white - 1] * fact.fact_inv[black + white];
      |                                                                ^~~~~~~~
a.cc: In function 'int main()':
a.cc:185:3: error: 'modint' was not declared in this scope; did you mean 'mode_t'?
  185 |   modint ans;
      |   ^~~~~~
      |   mode_t
a.cc:188:11: error: expected ';' before 'coeff'
  188 |     modint coeff(2 * empty + 2);
      |           ^~~~~~
      |           ;
a.cc:189:5: error: 'coeff' was not declared in this scope
  189 |     coeff -= modint(put - 2) * expected1(empty, put - empty - 1);
      |     ^~~~~
a.cc:191:5: error: 'ans' was not declared in this scope; did you mean 'abs'?
  191 |     ans += coeff * calc(put - 2, empty) * fact.fact[put] * fact.fact[empty];
      |     ^~~
      |     abs
a.cc:191:48: error: request for member 'fact' in 'fact', which is of non-class type 'int'
  191 |     ans += coeff * calc(put - 2, empty) * fact.fact[put] * fact.fact[empty];
      |                                                ^~~~
a.cc:191:65: error: request for member 'fact' in 'fact', which is of non-class type 'int'
  191 |     ans += coeff * calc(put - 2, empty) * fact.fact[put] * fact.fact[empty];
      |                                                                 ^~~~
a.cc:193:16: error: 'ans' was not declared in this scope; did you mean 'abs'?
  193 |   std::cout << ans << '\n';
      |                ^~~
      |                abs