Median Replace Hard

아직 제출이 없습니다시간 제한3초메모리 제한1024 MB

문제

Do you know this problem: https://atcoder.jp/contests/agc022/tasks/agc022_e? We generalize it a bit.

You got a binary string P=P_0P_1P_2P_3P_4P_5P_6P_7P = P\_0P\_1P\_2P\_3P\_4P\_5P\_6P\_7 of length 88.

You think a binary string XX of odd length NN is beautiful if it is possible to apply the following operation N12\frac{N-1}{2} times so that the only character of the resulting string is '1' :

  • Choose three consecutive bits of XX, (X_i,X_i+1,X_i+2)(X\_i,X\_{i + 1},X\_{i + 2}), and replace them by the (X_i+2X_i+1+4X_i+2)(X\_i + 2X\_{i + 1} + 4X\_{i + 2})-th bit of PP.

Note that, when P=00010111P = 00010111, this definition is the same as the original AGC problem.

You have a string SS consisting of characters '0', '1', and '?'. You want to know the number of ways to replace the question marks with '1' or '0' so that the resulting string is beautiful, modulo 109+710^9 + 7.

Note that there are TT tests in one input file.

입력

Input is given from Standard Input in the following format:

TT

PP SS

PP SS

\vdots

PP SS

출력

For each case, print the answer in a line.

제한

  • 1T2561 \leq T \leq 256
  • P=8|P|=8
  • 1S1 \leq |S| and S300,000\sum {|S|} \leq 300,000
  • S|S| is odd.
  • All characters of PP are either '0' or '1'.
  • All characters of SS are either '0', '1', or '?'.