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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Globalization;
using System.Diagnostics;



class Myon
{
    public Myon() { }
    public static int Main()
    {
        new Myon().calc();
        return 0;
    }


    Scanner cin;

    long mod = 1000000007;

    void calc()
    {
        cin = new Scanner();
        //string S = new string('?', 99999);//cin.next();
        string S = cin.next();
        int N = S.Length;
        long[,] dp = new long[N + 1, 5]; //0 -> 0start 1-> 0totyu 2->1start 3->1totyu 
        dp[0, 0] = 1;

        if(S.Length == 1)
        {
            if (S == "0") Console.WriteLine(0);
            else Console.WriteLine(1);
            return;
        }

        for (int i = 0; i < S.Length; i++)
        {
            if (S[i] == '?')
            {
                //4
                dp[i + 1, 4] += dp[i, 4] * 2;
                dp[i + 1, 4] %= mod;
            }
            else
            {
                dp[i + 1, 4] += dp[i, 4];
                dp[i + 1, 4] %= mod;
            }
            if (i%2 == 0)
            {
                //0
                int pattern1 = 1;
                if (S[i] == '?') pattern1 *= 2;

                int activePattern1 = pattern1;
                if(S[i] == '1' || S[i] == '?')
                {
                    dp[i + 1, 2] += dp[i, 0];
                    dp[i + 1, 2] %= mod;
                    activePattern1--;
                }
                dp[i + 1, 1] += dp[i, 0] * (activePattern1);
                dp[i + 1, 1] %= mod;

                //3
                if (i != S.Length - 1)
                {
                    int pattern2 = pattern1;
                    if (S[i + 1] == '?')
                    {
                        pattern2 *= 2;
                    }
                    int activePattern2 = pattern2;

                    if ((S[i] == '1' || S[i] == '?') && (S[i + 1] == '1' || S[i + 1] == '?'))
                    {
                        dp[i + 2, 4] += dp[i, 3];
                        dp[i + 2, 4] %= mod;
                        activePattern2--;
                    }
                    dp[i + 2, 3] += dp[i, 3] * activePattern2;
                    dp[i + 2, 3] %= mod;
                }
                else
                {
                    if (S[i] == '1' || S[i] == '?')
                    {
                        dp[i + 1, 4] += dp[i, 3];
                        dp[i + 1, 4] %= mod;
                    }
                }
            }
            else
            {

                int pattern1 = 1;
                if (S[i] == '?') pattern1 *= 2;

                //1

                if (i != S.Length - 2)
                {
                    int pattern2 = pattern1;
                    if (S[i + 1] == '?')
                    {
                        pattern2 *= 2;
                    }
                    int activePattern2 = pattern2;

                    if ((S[i] == '1' || S[i] == '?') && (S[i + 1] == '1' || S[i + 1] == '?'))
                    {
                        dp[i + 2, 2] += dp[i, 1];
                        dp[i + 2, 2] %= mod;
                        activePattern2--;
                    }
                    dp[i + 2, 1] += dp[i, 1] * activePattern2;
                    dp[i + 2, 1] %= mod;
                }
                else
                {
                    if ((S[i] == '1' || S[i] == '?') && (S[i + 1] == '1' || S[i + 1] == '?'))
                    {
                        dp[i + 1, 4] += dp[i, 1];
                        dp[i + 1, 4] %= mod;
                    }
                }


                //2
                int activePattern1 = pattern1;
                if (S[i] == '1' || S[i] == '?')
                {
                    dp[i + 1, 4] += dp[i, 2];
                    dp[i + 1, 4] %= mod;
                    activePattern1--;
                }
                dp[i + 1, 3] += dp[i, 2] * (activePattern1);
                dp[i + 1, 3] %= mod;
            }

        }

        for (int i = 0; i < N + 1; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                //Console.Write(dp[i,j] + " ");
            }
            //Console.WriteLine();
        }

        Console.WriteLine(dp[N, 4] % mod);
    }
}



class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }

    public int nextInt()
    {
        return int.Parse(next());
    }
    public int[] ArrayInt(int N, int add = 0)
    {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextInt() + add;
        }
        return Array;
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public long[] ArrayLong(int N, long add = 0)
    {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextLong() + add;
        }
        return Array;
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }


    public double[] ArrayDouble(int N, double add = 0)
    {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}