Problem p03013 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll memo[100001];

ll fract(ll num) {
  if (num == 1 || num == 0)
    return 1;
  if (memo[num] == -1)
    memo[num] = (num * fract(num - 1)) % 1000000007;
  return memo[num];
}

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) {
  int n, m;
  cin >> n >> m;
  vector<int> as;
  for (int i = 0; i < 100001; i++) {
    memo[i] = -1;
  }

  // cout << "koko" << endl;

  for (int i = 0; i < m; i++) {
    int t;
    cin >> t;
    as.push_back(t);
  }

  for (int i = 1; i < m; i++) {
    if (as[i] - 1 == as[i - 1]) {
      cout << 0 << endl;
      return 0;
    }
  }

  // cout << "koko0" << endl;

  int start = 0;
  ll ccnt = 1;
  for (int i = 0; i < m + 1; i++) {
    int sub = i < m ? as[i] - start - 1 : n - start;
    start = as[i] + 1;
    // cout << "sub:" << sub << endl;
    ll sum = 1;
    if (sub % 2 == 0) {
      // sum += 1;
      for (int j = 1; j <= sub / 2; j++) {
        sum +=
            fract(sub - j) * modinv(fract(j) * fract(sub - j * 2), 1000000007);
        sum %= 1000000007;
      }
      // cout << "koko1:" << sum << endl;
    } else {
      // sum += 1;
      for (int j = 1; j <= (sub - 1) / 2; j++) {
        sum +=
            fract(sub - j) * modinv(fract(j) * fract(sub - j * 2), 1000000007);
        sum %= 1000000007;
      }
      // cout << "koko2:" << sum << endl;
    }
    // cout << sum << endl;
    ccnt = (ccnt * sum) % 1000000007;
  }

  cout << ccnt << endl;

  return 0;
}

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

using namespace std;

typedef long long ll;

ll memo[100001];

ll fract(ll num) {
  if (num == 1 || num == 0)
    return 1;
  if (memo[num] == -1)
    memo[num] = (num * fract(num - 1)) % 1000000007;
  return memo[num];
}

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) {
  int n, m;
  cin >> n >> m;
  vector<int> as;
  for (int i = 0; i < 100001; i++) {
    memo[i] = -1;
  }

  // cout << "koko" << endl;

  for (int i = 0; i < m; i++) {
    int t;
    cin >> t;
    as.push_back(t);
  }

  for (int i = 1; i < m; i++) {
    if (as[i] - 1 == as[i - 1]) {
      cout << 0 << endl;
      return 0;
    }
  }

  // cout << "koko0" << endl;

  int start = 0;
  ll ccnt = 1;
  for (int i = 0; i < m + 1; i++) {
    int sub;
    if (m != 0) {
      sub = i < m ? as[i] - start - 1 : n - start;
      start = as[i] + 1;
    } else
      sub = n;
    // cout << "sub:" << sub << endl;
    ll sum = 1;
    if (sub % 2 == 0) {
      // sum += 1;
      for (int j = 1; j <= sub / 2; j++) {
        sum +=
            fract(sub - j) * modinv(fract(j) * fract(sub - j * 2), 1000000007);
        sum %= 1000000007;
      }
      // cout << "koko1:" << sum << endl;
    } else {
      // sum += 1;
      for (int j = 1; j <= (sub - 1) / 2; j++) {
        sum +=
            fract(sub - j) * modinv(fract(j) * fract(sub - j * 2), 1000000007);
        sum %= 1000000007;
      }
      // cout << "koko2:" << sum << endl;
    }
    // cout << sum << endl;
    ccnt = (ccnt * sum) % 1000000007;
  }

  cout << ccnt << endl;

  return 0;
}