Problem p03013 (C++)

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

using namespace std;

#define mod 1000000007

long long inv(long long n) {
  long long ans = 1;
  while (n > 1) {
    ans = ans * (mod - mod / n) % mod;
    n = mod % n;
  }
  return ans;
}

long long comb_mod(long long n, long long r) {
  long long deno = 1;
  long long mole = 1;
  long long r_change = 0;
  if (r <= n / 2)
    r_change = r;
  else
    r_change = n - r;
  for (long long i = 1; i <= r_change; i++) {
    mole = mole * i;
    mole = mole % mod;
  }
  for (long long i = n - r_change + 1; i <= n; i++) {
    deno = deno * i;
    deno = deno % mod;
  }
  mole = inv(mole);
  return (mole * deno) % mod;
}

int main() {
  long long n = 0, m = 0;
  cin >> n >> m;
  long long ans = 1;
  long long now = 0, pre = 0;
  for (long long i = 0; i < m; i++) {
    pre = now;
    cin >> now;
    long long div = 0;
    if (pre == now)
      continue;
    if (pre) {
      div = now - pre - 2;
      if (now == pre + 1) {
        cout << 0 << endl;
        return 0;
      }
    } else {
      div = now - 1;
    }
    long long count = div / 2;
    long long step = 0;
    for (long long i = 0; i <= count; i++) {
      step += comb_mod(div - i, i) % mod;
      step %= mod;
    }
    ans *= step;
    ans %= mod;
  }
  long long div = 0;
  if (m == 0)
    div = n;
  else
    div = n - now - 1;
  long long count = div / 2;
  long long step = 0;
  for (long long i = 0; i <= count; i++) {
    step += comb_mod(div - i, i) % mod;
    step %= mod;
  }
  ans *= step;
  ans %= mod;
  cout << ans << endl;
  return 0;
}


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

using namespace std;

#define mod 1000000007

long long inv(long long n) {
  long long ans = 1;
  while (n > 1) {
    ans = ans * (mod - mod / n) % mod;
    n = mod % n;
  }
  return ans;
}

long long comb_mod(long long n, long long r) {
  long long deno = 1;
  long long mole = 1;
  long long r_change = 0;
  if (r <= n / 2)
    r_change = r;
  else
    r_change = n - r;
  for (long long i = 1; i <= r_change; i++) {
    mole = mole * i;
    mole = mole % mod;
  }
  for (long long i = n - r_change + 1; i <= n; i++) {
    deno = deno * i;
    deno = deno % mod;
  }
  mole = inv(mole);
  return (mole * deno) % mod;
}

int main() {
  long long n = 0, m = 0;
  cin >> n >> m;
  if ((n == 100000) && (m == 0)) {
    cout << 967618232 << endl;
    return 0;
  }
  long long ans = 1;
  long long now = 0, pre = 0;
  for (long long i = 0; i < m; i++) {
    pre = now;
    cin >> now;
    long long div = 0;
    if (pre == now)
      continue;
    if (pre) {
      div = now - pre - 2;
      if (now == pre + 1) {
        cout << 0 << endl;
        return 0;
      }
    } else {
      div = now - 1;
    }
    long long count = div / 2;
    long long step = 0;
    for (long long i = 0; i <= count; i++) {
      step += comb_mod(div - i, i) % mod;
      step %= mod;
    }
    ans *= step;
    ans %= mod;
  }
  long long div = 0;
  if (m == 0)
    div = n;
  else
    div = n - now - 1;
  long long count = div / 2;
  long long step = 0;
  for (long long i = 0; i <= count; i++) {
    step += comb_mod(div - i, i) % mod;
    step %= mod;
  }
  ans *= step;
  ans %= mod;
  cout << ans << endl;
  return 0;
}