Problem p03397 (C++)

Buggy submission (status: Runtime Error):
// BadWaper gg
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define inf 1e9
#define eps 1e-6
#define mp make_pair
#define N 100010
#define M 3
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read() {
  char ch = getchar();
  ll s = 0, w = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}
int f[N][M][M];
int n;
char s[N];
const int mod = 1e9 + 7;
inline int Z(int x) { return x >= mod ? x - mod : x; }
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  scanf("%s", s + 1);
  n = strlen(s + 1);
  f[0][0][0] = 1;
  for (register int i = 0; i < n; i++) {
    for (register int j = 0; j < 3; j++) {
      for (register int k = 0; k < 3; k++) {
        if (!f[i][j][k])
          continue;
        if (s[i + 1] != '0') {
          if (k) {
            f[i + 1][j][k - 1] = Z(f[i + 1][j][k - 1] + f[i][j][k]);
          } else
            f[i + 1][min(j + 1, 2)][k] =
                Z(f[i + 1][min(j + 1, 2)][k] + f[i][j][k]);
        }
        if (s[i + 1] != '1') {
          if (k == 2) {
            f[i + 1][j][1] = Z(f[i + 1][j][1] + f[i][j][k]);
          } else
            f[i + 1][j][k + 1] = Z(f[i + 1][j][k + 1] + f[i][j][k]);
        }
      }
    }
  }
  int ans = 0;
  for (register int i = 0; i < 3; i++) {
    for (register int j = 0; j <= i; j++)
      ans = Z(ans + f[n][i][j]);
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
// BadWaper gg
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define inf 1e9
#define eps 1e-6
#define mp make_pair
#define N 300010
#define M 3
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read() {
  char ch = getchar();
  ll s = 0, w = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}
int f[N][M][M];
int n;
char s[N];
const int mod = 1e9 + 7;
inline int Z(int x) { return x >= mod ? x - mod : x; }
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  scanf("%s", s + 1);
  n = strlen(s + 1);
  f[0][0][0] = 1;
  for (register int i = 0; i < n; i++) {
    for (register int j = 0; j < 3; j++) {
      for (register int k = 0; k < 3; k++) {
        if (!f[i][j][k])
          continue;
        if (s[i + 1] != '0') {
          if (k) {
            f[i + 1][j][k - 1] = Z(f[i + 1][j][k - 1] + f[i][j][k]);
          } else
            f[i + 1][min(j + 1, 2)][k] =
                Z(f[i + 1][min(j + 1, 2)][k] + f[i][j][k]);
        }
        if (s[i + 1] != '1') {
          if (k == 2) {
            f[i + 1][j][1] = Z(f[i + 1][j][1] + f[i][j][k]);
          } else
            f[i + 1][j][k + 1] = Z(f[i + 1][j][k + 1] + f[i][j][k]);
        }
      }
    }
  }
  int ans = 0;
  for (register int i = 0; i < 3; i++) {
    for (register int j = 0; j <= i; j++)
      ans = Z(ans + f[n][i][j]);
  }
  printf("%d\n", ans);
  return 0;
}