Under construction

Still being built. Things may change

Making a Beautiful Puzzle

Time limit3sMemory limit128 MB

Summary
Fill each square of an N by M board with one of four colors so that orthogonal neighbors differ, maximizing total beauty and counting optimal placements modulo 1e9+7.
Level

Hard8 of 10

Topics
Dynamic programming, Backtracking, Combinatorics, Implementation
Solved
No attempts yet

Problem

For his birthday Seongyong got an N×MN \times M puzzle board and 1×11 \times 1 puzzle pieces in red, blue, green and yellow. He has infinitely many pieces of each color.

Seongyong puts exactly one piece on every square of the board. He wants the board to look colorful, so two squares that touch horizontally or vertically always hold pieces of different colors.

Putting a piece of a given color on a given square earns the beauty fixed in advance for that square and that color. The beauty of the whole puzzle is the sum of the beauty earned on every square.

Write a program that finds the largest possible beauty of the whole puzzle and the number of placements that reach it.

Input

The first line contains NN and MM, separated by a space. (1≤N≤101 \le N \le 10, 1≤M≤101 \le M \le 10)

The next NN lines contain the beauty earned by putting a red piece on each square, MM values per line.

The following NN lines give the values for blue pieces, the next NN lines give the values for green pieces, and the last NN lines give the values for yellow pieces, in the same format.

Every beauty is an integer between 00 and 10910^9. The largest possible beauty of the whole puzzle does not exceed 2,100,000,000.

Output

On the first line print the largest possible beauty of the whole puzzle and the number of placements that reach it, separated by a space. The number of placements can be very large, so print it modulo 109+710^9 + 7.

Hint

Write the four colors as R, B, G and Y.

In the first example the board has a single square, and every color gives a total beauty of 1, so 1 is the maximum. There are four placements.

In the second example, putting R and B on the top row and G and Y on the bottom row gives 2+2+2+2=82 + 2 + 2 + 2 = 8, which is the maximum. Only one placement reaches it.

Examples2

  1. Example 1

    Input
    1 1
    1
    1
    1
    1
    
    Expected output
    1 4
    
  2. Example 2

    Input
    2 2
    2 1
    1 1
    1 2
    1 1
    1 1
    2 1
    1 1
    1 2
    
    Expected output
    8 1