This page is still under construction.

Parts of this page are still being built. What you see may change.

Split and Merge

Time limit1sMemory limit512 MB

Summary
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.
Level

Hard8 of 10

Topics
Dynamic programming, Combinatorics, String matching, Prefix sum
Solved
No attempts yet

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 (1≤L≤30001 \le L \le 3000).

The second line gives the number of pieces nn in the initial state (1≤n≤L1 \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 (1≤m≤L1 \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.

Examples1

  1. Example 1

    Input
    6
    5
    1 2 1 1 1
    4
    2 1 2 1
    Expected output
    3 3