#P12607. 三叉求和

三叉求和

Description

You are given an infinite ternary tree rooted at vertex 00. The children of vertex ii are the vertices 3i+13i+1, 3i+23i+2, and 3i+33i+3.

Let aia_i denote the value written on vertex ii. It is guaranteed that for all i0i \ge 0 and 0j20 \le j \le 2, we have a3i+j+1=3×ai+ja_{3i + j + 1} = 3 \times a_i + j.

Your task is to count the number of simple paths with length equal to dd starting at vertex 00 such that the sum of the values along the path equals kk.

However, kk is not completely specified: some digits in its ternary representation are replaced with the character ??, indicating unknown digits.

Your task is to compute the total number of such paths across all valid values of kk, considering all ways to replace each ?? with a digit from 0,1,2{0,1,2}.

Input Format

The first line contains a single integer dd.

Next line contains the ternary representation of kk. The ternary string may contain leading zeros and question marks, and its length is exactly dd.

Output Format

Print a single integer — the total number of valid simple paths modulo 109+710^9 + 7.

3
?12
3
3
???
19
4
0211
1
10
21??1?2??0
161
30
???1????0????1???0????2???????
744432249

Hint

Sample Explanation #1

The legal paths are:

  • [0,1,5,17][0,1,5,17]
  • [0,2,7,23][0,2,7,23]
  • [0,2,9,30][0,2,9,30]

These paths correspond to the following sequences of vertex values:

  • [0,0,1,4][0,0,1,4]
  • [0,1,3,10][0,1,3,10]
  • [0,1,5,17][0,1,5,17]

Data Range

TestCase # dd Special Constraints
121\sim 2 15\leq15 -
33 50\leq50 A
44 B
585\sim 8 -
9109\sim 10 300\leq300 A
111211\sim 12 B
131413\sim 14 -
1515 2000\leq2000 A
1616 B
172017\sim 20 -

A: It is guaranteed that the number of ??s in kk is no more than 11.

B: kk only contains ??.

It is guaranteed that for all testcases, 1d2000,0k<3d+11\le d\le 2000,0\le k\lt 3^{d+1}.