Problem p03013 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cctype>
#include <climits>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

// a と b の最大公約数を返す関数
long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}

long long gcd(long long a, long long b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

long long lcm(long long a, long long b) { return a * b / gcd(a, b); }

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
} comb(2000005);

mint g(int r, int c) { return comb(r + c + 2, r + 1) - 1; }

bool comp(int i, int j) { return i > j; }

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  return a.second < b.second;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> broken;
  for (int i = 0; i < m; i++) {
    int tmp;
    cin >> tmp;
    broken.push_back(tmp);
  }

  long long count[100001];
  int brokenCount = 0;
  for (int i = 0; i <= n; i++) {
    if (i == 0) {
      count[i] = 1;
    } else if (i == 1) {
      if (broken[0] == 1) {
        count[i] = 0;
        brokenCount++;
      } else {
        count[i] = count[i - 1];
      }
    } else {
      if (brokenCount < broken.size() && i == broken[brokenCount]) {
        count[i] = 0;
        brokenCount++;
      } else {
        count[i] = count[i - 1] + count[i - 2];
        count[i] %= 1000000007;
      }
    }
  }
  cout << count[n];
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cctype>
#include <climits>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

// a と b の最大公約数を返す関数
long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}

long long gcd(long long a, long long b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

long long lcm(long long a, long long b) { return a * b / gcd(a, b); }

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
} comb(2000005);

mint g(int r, int c) { return comb(r + c + 2, r + 1) - 1; }

bool comp(int i, int j) { return i > j; }

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  return a.second < b.second;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> broken;
  for (int i = 0; i < m; i++) {
    int tmp;
    cin >> tmp;
    broken.push_back(tmp);
  }

  long long count[100001];
  int brokenCount = 0;
  for (int i = 0; i <= n; i++) {
    if (i == 0) {
      count[i] = 1;
    } else if (i == 1) {
      if (brokenCount < broken.size() && broken[0] == 1) {
        count[i] = 0;
        brokenCount++;
      } else {
        count[i] = count[i - 1];
      }
    } else {
      if (brokenCount < broken.size() && i == broken[brokenCount]) {
        count[i] = 0;
        brokenCount++;
      } else {
        count[i] = count[i - 1] + count[i - 2];
        count[i] %= 1000000007;
      }
    }
  }
  cout << count[n];
}