Problem:
Score : 1600 points

Problem Statement

Taichi thinks a binary string X of odd length N is beautiful if it is possible to apply the following operation \frac{N-1}{2} times so that the only character of the resulting string is 1 :

Choose three consecutive bits of X and replace them by their median. For example, we can turn 00110 into 010 by applying the operation to the middle three bits.

Taichi has a string S consisting of characters 0, 1 and ?. Taichi wants to know the number of ways to replace the question marks with 1 or 0 so that the resulting string is beautiful, modulo 10^{9} + 7.

Constraints

1 \leq |S| \leq 300000

|S| is odd.

All characters of S are either 0, 1 or ?.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of ways to replace the question marks so that the resulting string is beautiful, modulo 10^{9} + 7.

Sample Input 1

1??00

Sample Output 1

2

There are 4 ways to replace the question marks with 0 or 1 :

11100 : This string is beautiful because we can first perform the operation on the last 3 bits to get 110 and then on the only 3 bits to get 1.

11000 : This string is beautiful because we can first perform the operation on the last 3 bits to get 110 and then on the only 3 bits to get 1.

10100 : This string is not beautiful because there is no sequence of operations such that the final string is 1.

10000 : This string is not beautiful because there is no sequence of operations such that the final string is 1.

Thus, there are 2 ways to form a beautiful string.

Sample Input 2

?

Sample Output 2

1

In this case, 1 is the only beautiful string.

Sample Input 3

?0101???10???00?1???????????????0????????????1????0

Sample Output 3

402589311

Remember to output your answer modulo 10^{9} + 7.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i = (a); i <= (b); ++ i)
#define rrp(i,a,b) for (int i = (a); i >= (b); -- i)
#define gc() getchar()
#define fir first
#define sec second
typedef pair<int,int> pii;
typedef double db;
typedef long long ll;
template <typename tp>
inline void read(tp& x) {
  x = 0; char tmp; bool key = 0;
  for (tmp = gc(); !isdigit(tmp); tmp = gc())
    key = (tmp == '-');
  for (; isdigit(tmp); tmp = gc())
    x = (x << 3) + (x << 1) + (tmp ^ '0');
  if (key) x = -x;
}
template <typename tp>
inline void ckmn(tp& x,tp y) {
  x = x < y ? x : y;
}
template <typename tp>
inline void ckmx(tp& x,tp y) {
  x = x < y ? y : x;
}

const int N = 300010, MOD = (int)(1e9 + 7);
inline void Add(int& x,int y) {
  x = x + y >= MOD ? x + y - MOD : x + y;
}
inline void Sub(int& x,int y) {
  x = x - y < 0 ? x - y + MOD : x - y;
}
char str[N];
int n, dp[N][2][3][3], ans;
int main() {
  scanf("%s", str + 1);
  n = strlen(str + 1);
  dp[0][0][0][0] = 1;
  rep (i, 1, n) {
    if (str[i] == '?' || str[i] == '0') {
      rep (a, 0, 1) rep (b, 0, 2) rep (c, 0, 2) if (dp[i-1][a][b][c]) {
	int na = a, nb = (b == 0 ? 1 : 3 - b), nc = c;
	if (i == n) {
	  if (nb == 0 || nb == 2) {
	    na ^= 1;
	    if (nc == 0 && na == 1 && nb == 0) ++ nc;
	    else if (nc == 1 && na == 0 && nb == 0) ++ nc;
	  }
	  nb = 0;
	}
	Add(dp[i][na][nb][nc], dp[i-1][a][b][c]);
      }
    }
    if (str[i] == '?' || str[i] == '1') {
      rep (a, 0, 1) rep (b, 0, 2) rep (c, 0, 2) if (dp[i-1][a][b][c]) {
	int na = a, nb = b, nc = c;
	if (nb == 0 || nb == 2) {
	  na ^= 1;
	  if (nc == 0 && na == 1 && nb == 0) ++ nc;
	  else if (nc == 1 && na == 0 && nb == 0) ++ nc;
	}
	nb = 0;
	if (i == n) {
	  na ^= 1;
	  if (nc == 0 && na == 1) ++ nc;
	  else if (nc == 1 && na == 0) ++ nc;	  
	}
	Add(dp[i][na][nb][nc], dp[i-1][a][b][c]);
      }
    }
  }
  rep (a, 0, 1) rep (b, 0, 2)
    Add(ans, dp[n][a][b][2]);
  cout << ans << endl;
  return 0;
}