Problem p03748 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXSIZE = 30000020;
const int mod = 1000000007;
int bufpos;
char buf[MAXSIZE];
#define NEG 0
void init() {
  freopen("piling.in", "r", stdin);
  freopen("piling.out", "w", stdout);
  buf[fread(buf, 1, MAXSIZE, stdin)] = '\0';
  bufpos = 0;
}
#if NEG
int readint() {
  bool isneg;
  int val = 0;
  for (; !isdigit(buf[bufpos]) && buf[bufpos] != '-'; bufpos++)
    ;
  bufpos += (isneg = buf[bufpos] == '-');
  for (; isdigit(buf[bufpos]); bufpos++)
    val = val * 10 + buf[bufpos] - '0';
  return isneg ? -val : val;
}
#else
int readint() {
  int val = 0;
  for (; !isdigit(buf[bufpos]); bufpos++)
    ;
  for (; isdigit(buf[bufpos]); bufpos++)
    val = val * 10 + buf[bufpos] - '0';
  return val;
}
#endif
char readchar() {
  for (; isspace(buf[bufpos]); bufpos++)
    ;
  return buf[bufpos++];
}
int readstr(char *s) {
  int cur = 0;
  for (; isspace(buf[bufpos]); bufpos++)
    ;
  for (; !isspace(buf[bufpos]); bufpos++)
    s[cur++] = buf[bufpos];
  s[cur] = '\0';
  return cur;
}
int f[3003][2], g[3003][2];
inline void add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
int main() {
  init();
  int n = readint(), m = readint() - 1;
  for (int i = 0; i < n; i++)
    f[i][i == 0] = 1;
  for (int i = 0; i < m; i++) {
    memset(g, 0, sizeof(g));
    for (int j = 0; j < n; j++) {
      add(g[j + 1][0], f[j][0]);
      add(g[j + 1][1], f[j][1]);
      add(g[j][0], f[j][0]);
      add(g[j][0], f[j][0]);
      add(g[j][1], f[j][1]);
      add(g[j][1], f[j][1]);
      if (j) {
        add(g[j - 1][j == 1], f[j][0]);
        add(g[j - 1][1], f[j][1]);
      }
    }
    swap(f, g);
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    add(ans, f[i][1]);
  add(ans, ans);
  add(ans, ans);
  printf("%d", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXSIZE = 30000020;
const int mod = 1000000007;
int bufpos;
char buf[MAXSIZE];
#define NEG 0
void init() {
  // freopen("piling.in","r",stdin);
  // freopen("piling.out","w",stdout);
  buf[fread(buf, 1, MAXSIZE, stdin)] = '\0';
  bufpos = 0;
}
#if NEG
int readint() {
  bool isneg;
  int val = 0;
  for (; !isdigit(buf[bufpos]) && buf[bufpos] != '-'; bufpos++)
    ;
  bufpos += (isneg = buf[bufpos] == '-');
  for (; isdigit(buf[bufpos]); bufpos++)
    val = val * 10 + buf[bufpos] - '0';
  return isneg ? -val : val;
}
#else
int readint() {
  int val = 0;
  for (; !isdigit(buf[bufpos]); bufpos++)
    ;
  for (; isdigit(buf[bufpos]); bufpos++)
    val = val * 10 + buf[bufpos] - '0';
  return val;
}
#endif
char readchar() {
  for (; isspace(buf[bufpos]); bufpos++)
    ;
  return buf[bufpos++];
}
int readstr(char *s) {
  int cur = 0;
  for (; isspace(buf[bufpos]); bufpos++)
    ;
  for (; !isspace(buf[bufpos]); bufpos++)
    s[cur++] = buf[bufpos];
  s[cur] = '\0';
  return cur;
}
int f[3003][2], g[3003][2];
inline void add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
int main() {
  init();
  int n = readint(), m = readint() - 1;
  for (int i = 0; i < n; i++)
    f[i][i == 0] = 1;
  for (int i = 0; i < m; i++) {
    memset(g, 0, sizeof(g));
    for (int j = 0; j < n; j++) {
      add(g[j + 1][0], f[j][0]);
      add(g[j + 1][1], f[j][1]);
      add(g[j][0], f[j][0]);
      add(g[j][0], f[j][0]);
      add(g[j][1], f[j][1]);
      add(g[j][1], f[j][1]);
      if (j) {
        add(g[j - 1][j == 1], f[j][0]);
        add(g[j - 1][1], f[j][1]);
      }
    }
    swap(f, g);
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    add(ans, f[i][1]);
  add(ans, ans);
  add(ans, ans);
  printf("%d", ans);
}