Problem: Have you ever heard of Moduic Squares? They are like 3 × 3 Magic Squares, but each of them has one extra cell called a moduic cell. Hence a Moduic Square has the following form.

<image>

Figure 1: A Moduic Square

Each of cells labeled from A to J contains one number from 1 to 10, where no two cells contain the same number. The sums of three numbers in the same rows, in the same columns, and in the diagonals in the 3 × 3 cells must be congruent modulo the number in the moduic cell. Here is an example Moduic Square:

<image>

Figure 2: An example Moduic Square

You can easily see that all the sums are congruent to 0 modulo 5.

Now, we can consider interesting puzzles in which we complete Moduic Squares with partially filled cells. For example, we can complete the following square by filling 4 into the empty cell on the left and 9 on the right. Alternatively, we can also complete the square by filling 9 on the left and 4 on the right. So this puzzle has two possible solutions.

<image>

Figure 3: A Moduic Square as a puzzle

Your task is to write a program that determines the number of solutions for each given puzzle.



Input

The input contains multiple test cases. Each test case is specified on a single line made of 10 integers that represent cells A, B, C, D, E, F, G, H, I, and J as shown in the first figure of the problem statement. Positive integer means that the corresponding cell is already filled with the integer. Zero means that the corresponding cell is left empty.

The end of input is identified with a line containing ten of -1’s. This is not part of test cases.

Output

For each test case, output a single integer indicating the number of solutions on a line. There may be cases with no solutions, in which you should output 0 as the number.

Example

Input

3 1 6 8 10 7 0 0 2 5
0 0 0 0 0 0 0 0 0 1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1


Output

2
362880

Solution (in JAVA):

Solution:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.TreeSet;
 
 
public class Main{
     
	public static boolean check(int pos, int[] array, int mod){
		if(pos < 2){
			return true;
		}
		
		int m = 0;
		if(pos >= 2){
			m = (array[0] + array[1] + array[2]) % mod;
		}
		
		if(pos >= 5 && ((array[3] + array[4] + array[5]) % mod != m)){
			return false;
		}
		
		if(pos >= 6 && (((array[0] + array[3] + array[6]) % mod != m) || (array[2] + array[4] + array[6]) % mod != m)){
			return false;
		}
		
		if(pos >= 7 && ((array[1] + array[4] + array[7]) % mod != m)){
			return false;
		}
		
		if(pos >= 8 && (((array[2] + array[5] + array[8]) % mod != m) || (array[0] + array[4] + array[8]) % mod != m)){
			return false;
		}
		
		return true;
	}
	
	public static int dfs(int pos, int[] array, boolean[] used, int mod){
		//System.out.println(pos);
		if(pos < 9){
			if(array[pos] != 0){
				if(used[array[pos]]){
					return 0;
				}else if(!check(pos, array, mod)){
					return 0;
				}else{
					used[array[pos]] = true;
					int ret = dfs(pos + 1, array, used, mod);
					used[array[pos]] = false;
					
					return ret;
				}
			}else{
				int sum = 0;
				
				for(int i = 1; i <= 10; i++){
					//System.out.println(Arrays.toString(used));
					if(used[i]){
						continue;
					}else{
						used[i] = true;
						array[pos] = i;
							
						if(!check(pos, array, mod)){
							array[pos] = 0;
							used[i] = false;
							continue;
						}
						sum += dfs(pos + 1, array, used, mod);
						array[pos] = 0;
						used[i] = false;
					}
				}
				
				return sum;
			}
		}else{
			return 1;
		}
	}
	
    public static void main(String[] args) throws IOException {
    	Scanner sc = new Scanner(System.in);
    	
    	while(true){
    		int[] array = new int[10];
    		
    		for(int i = 0; i < 10; i++){
    			array[i] = sc.nextInt();
    		}
    		
    		boolean flag = true;
    		for(int i = 0; i < 10; i++){
    			if(array[i] != -1){
    				flag = false;
    				break;
    			}
    		}
    		
    		if(flag){
    			break;
    		}
    		
    		if(array[9] != 0){
    			boolean[] used = new boolean[11];
    			used[array[9]] = true;
    			System.out.println(dfs(0, array, used, array[9]));
    		}else{
    			int sum = 0;
    			boolean[] used = new boolean[11];
    			for(int i = 1; i <= 10; i++){
    				used[i] = true;
    				sum += dfs(0, array, used, i);
    				used[i] = false;
    			}
    			System.out.println(sum);
    		}
    	}
    }
     
}