#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <sstream>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it =(c).begin(); it!=(c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))
 
#define R(i)((m[way[i][0]] + m[way[i][1]] + m[way[i][2]]) % m[9]) 

using namespace std; 

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;

const int way[8][3] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6} };

int res;
int m[10];

void dfs(int);

int main(void){
    for(;cin >> m[0], ~m[0]; cout << res << endl){
        COUNT(i, 9) cin >> m[i];

        res = 0;
        dfs(0);
    }

    return 0;
}

void dfs(int num){
    if(num == 10){
        int r = R(0), inc = 1;
        COUNT(i, 7) inc &= R(i) == r;
        res += inc;
        return;
    }

    if(!m[num]){
        COUNT(i, 10) if(find(m, m + 10, i) == m + 10) m[num] = i, dfs(num + 1);
        m[num] = 0;
    }
    else dfs(num + 1);
}
### Prompt

Your challenge is to write a Cpp solution to the following 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

### Response

```cpp
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <sstream>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it =(c).begin(); it!=(c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))
 
#define R(i)((m[way[i][0]] + m[way[i][1]] + m[way[i][2]]) % m[9]) 

using namespace std; 

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;

const int way[8][3] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6} };

int res;
int m[10];

void dfs(int);

int main(void){
    for(;cin >> m[0], ~m[0]; cout << res << endl){
        COUNT(i, 9) cin >> m[i];

        res = 0;
        dfs(0);
    }

    return 0;
}

void dfs(int num){
    if(num == 10){
        int r = R(0), inc = 1;
        COUNT(i, 7) inc &= R(i) == r;
        res += inc;
        return;
    }

    if(!m[num]){
        COUNT(i, 10) if(find(m, m + 10, i) == m + 10) m[num] = i, dfs(num + 1);
        m[num] = 0;
    }
    else dfs(num + 1);
}
```