Problem p03013 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>

static const int IINF = 1 << 30;
static const long long LINF = 1LL << 60;
static const long long MOD = 1.0e+9 + 7;

template <typename T> std::vector<T> vectors(std::size_t n, T val) {
  return std::vector<T>(n, val);
}

template <typename T, typename... Args>
auto vectors(std::size_t n, Args... args) {
  return std::vector<decltype(vectors<T>(args...))>(n, vectors<T>(args...));
}

template <class T> inline bool chmin(T &a, const T &b) {
  return (a > b) ? a = b, true : false;
}

template <class T> inline bool chmax(T &a, const T &b) {
  return (a < b) ? a = b, true : false;
}

template <class T> inline void chadd(T &a, const T &b) {
  a += b, a %= MOD;
  // TODO minus case
}

template <class T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) {
  if (v.empty())
    return s;
  s << *v.begin();
  for (auto iter = v.begin() + 1; iter != v.end(); ++iter)
    if (std::is_fundamental<T>::value)
      s << " " << *iter;
    else
      s << std::endl << *iter;
  return s;
}

int main() {
  // Input
  int N, M;
  std::cin >> N >> M;
  std::vector<int> a(M);
  for (int i = 0; i < M; ++i)
    std::cin >> a[i];

  // Main
  std::vector<long long> f(1000000, 0);
  f[0] = 0;
  f[1] = 1;
  f[2] = 1;
  for (int i = 3; i <= N + 1; ++i) {
    f[i] = f[i - 1] + f[i - 2];
    f[i] %= MOD;
  }

  long long res = 1;
  for (int i = 0; i < M; ++i) {
    if (i == 0) {
      int d = a[0];
      assert(d >= 0);
      res *= f[d];
      res %= MOD;
      continue;
    }
    if (a[i] == a[i - 1])
      continue;
    int d = a[i] - a[i - 1] - 1;
    assert(d >= 0);
    res *= f[d];
    res %= MOD;
  }
  int d = N - a[M - 1];
  assert(d >= 0);
  res *= f[d];
  res %= MOD;

  // Output
  std::cout << res << std::endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>

static const int IINF = 1 << 30;
static const long long LINF = 1LL << 60;
static const long long MOD = 1.0e+9 + 7;

template <typename T> std::vector<T> vectors(std::size_t n, T val) {
  return std::vector<T>(n, val);
}

template <typename T, typename... Args>
auto vectors(std::size_t n, Args... args) {
  return std::vector<decltype(vectors<T>(args...))>(n, vectors<T>(args...));
}

template <class T> inline bool chmin(T &a, const T &b) {
  return (a > b) ? a = b, true : false;
}

template <class T> inline bool chmax(T &a, const T &b) {
  return (a < b) ? a = b, true : false;
}

template <class T> inline void chadd(T &a, const T &b) {
  a += b, a %= MOD;
  // TODO minus case
}

template <class T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) {
  if (v.empty())
    return s;
  s << *v.begin();
  for (auto iter = v.begin() + 1; iter != v.end(); ++iter)
    if (std::is_fundamental<T>::value)
      s << " " << *iter;
    else
      s << std::endl << *iter;
  return s;
}

int main() {
  // Input
  int N, M;
  std::cin >> N >> M;
  std::vector<int> a(M);
  for (int i = 0; i < M; ++i)
    std::cin >> a[i];

  // Main
  std::vector<long long> f(1000000, 0);
  f[0] = 0;
  f[1] = 1;
  f[2] = 1;
  for (int i = 3; i <= N + 1; ++i) {
    f[i] = f[i - 1] + f[i - 2];
    f[i] %= MOD;
  }

  long long res = 1;
  for (int i = 0; i < M; ++i) {
    if (i == 0) {
      int d = a[0];
      assert(d >= 0);
      res *= f[d];
      res %= MOD;
      continue;
    }
    if (a[i] == a[i - 1])
      continue;
    int d = a[i] - a[i - 1] - 1;
    assert(d >= 0);
    res *= f[d];
    res %= MOD;
  }
  int d;
  if (M >= 1)
    d = N - a[M - 1];
  else
    d = N + 1;
  assert(d >= 0);
  res *= f[d];
  res %= MOD;

  // Output
  std::cout << res << std::endl;

  return 0;
}