Problem p03013 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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; }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  rep(i, m) {
    int b;
    cin >> b;
    b--;
    a[i] = b;
  }
  int b = 0;
  if (m > 0)
    b = a[0];
  int j = 1;
  vector<mint> dp(3);
  dp[2] = 1;
  rep(i, n) {
    vector<mint> p(3);
    swap(dp, p);
    dp[0] = p[1];
    dp[1] = p[2];
    if (b == i) {
      dp[2] = 0;
      b = a[j];
      if (j < m - 1)
        j++;
    } else {
      dp[2] = dp[0] + dp[1];
    }
  }
  cout << dp[2].x << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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; }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  rep(i, m) {
    int b;
    cin >> b;
    b--;
    a[i] = b;
  }
  int b = -1;
  if (m > 0)
    b = a[0];
  int j = 1;
  vector<mint> dp(3);
  dp[2] = 1;
  rep(i, n) {
    vector<mint> p(3);
    swap(dp, p);
    dp[0] = p[1];
    dp[1] = p[2];
    if (b == i) {
      dp[2] = 0;
      b = a[j];
      if (j < m - 1)
        j++;
    } else {
      dp[2] = dp[0] + dp[1];
    }
  }
  cout << dp[2].x << endl;
  return 0;
}