Split and Merge

Given two tilings of a 1xL board by 1x1 and 1x2 pieces, find the minimum number of split/merge operations to transform one into the other and count the ways.

Hard8Dynamic programmingCombinatoricsString matchingPrefix sumNo attempts yetTime limit1sMemory limit512 MB

Problem

A 1×L1 \times L board is divided into 1×11 \times 1 pieces and 1×21 \times 2 pieces.

You can perform two kinds of operations. One splits a single 1×21 \times 2 piece into two 1×11 \times 1 pieces. The other merges two adjacent 1×11 \times 1 pieces into a single 1×21 \times 2 piece.

Given the initial state and the target state of the board, find the minimum number of operations and the number of ways to change the state with that many operations.

Input

The first line gives the board length LL (1L30001 \le L \le 3000).

The second line gives the number of pieces nn in the initial state (1nL1 \le n \le L).

The third line gives nn numbers a1,a2,...,ana_1, a_2, ..., a_n. They list each piece length from the leftmost piece, and each number is 1 or 2. The nn numbers sum to LL.

The fourth line gives the number of pieces mm in the target state (1mL1 \le m \le L).

The fifth line gives mm numbers b1,b2,...,bmb_1, b_2, ..., b_m in the same format as the third line.

Output

Print the minimum number of operations and the number of ways, separated by a space. Since the number of ways can be large, print it modulo 10000000071000000007.