Problem p03013 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (ll i = n; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const int INF = 1e9;
const ll LLINF = 1e16;

ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main(void) {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(m);
  vector<ll> range(m + 1);
  ll mod = 1e9 + 7;

  REP(i, m) { cin >> a[i]; }

  range[0] = a[0] - 1;
  range[m] = n - a[m - 1] - 1;

  REP(i, m - 1) {
    if (a[i + 1] - a[i] == 1) {
      cout << 0 << endl;
      return 0;
    }
  }
  FOR(i, 1, m) { range[i] = a[i] - a[i - 1] - 2; }

  ll max = *max_element(all(range));

  vector<ll> mod_kaijo(max + 1);
  mod_kaijo[0] = 1;

  FOR(i, 1, max + 1) { mod_kaijo[i] = ((i % mod) * mod_kaijo[i - 1]) % mod; }

  ll ans = 1;
  REP(i, range.size()) {
    ll sum = 0;
    for (ll j = 0; j <= range[i] / 2; j++) {
      sum = (sum +
             (mod_kaijo[range[i] - j] *
              modinv((mod_kaijo[range[i] - 2 * j] * mod_kaijo[j]) % mod, mod)) %
                 mod) %
            mod;
    }
    ans = (ans * sum) % mod;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (ll i = n; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const int INF = 1e9;
const ll LLINF = 1e16;

ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main(void) {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(m);
  vector<ll> range(m + 1);
  ll mod = 1e9 + 7;

  REP(i, m) { cin >> a[i]; }

  if (m != 0) {
    range[0] = a[0] - 1;
    range[m] = n - a[m - 1] - 1;
  } else {
    range[0] = n;
  }

  REP(i, m - 1) {
    if (a[i + 1] - a[i] == 1) {
      cout << 0 << endl;
      return 0;
    }
  }
  FOR(i, 1, m) { range[i] = a[i] - a[i - 1] - 2; }

  ll max = *max_element(all(range));

  vector<ll> mod_kaijo(max + 1);
  mod_kaijo[0] = 1;

  FOR(i, 1, max + 1) { mod_kaijo[i] = ((i % mod) * mod_kaijo[i - 1]) % mod; }

  ll ans = 1;
  REP(i, range.size()) {
    ll sum = 0;
    for (ll j = 0; j <= range[i] / 2; j++) {
      sum = (sum +
             (mod_kaijo[range[i] - j] *
              modinv((mod_kaijo[range[i] - 2 * j] * mod_kaijo[j]) % mod, mod)) %
                 mod) %
            mod;
    }
    ans = (ans * sum) % mod;
  }

  cout << ans << endl;

  return 0;
}