Problem:
Problem A: Moduic Squares

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.

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:

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.

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.

Sample 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 for the Sample Input

2
362880

Solution in C (judge verdict: Runtime Error)

// AOJ 2058: Moduic Squares
// 2017.11.6 bal4u@uu

#include <stdio.h>
#include <string.h>
#define TM 0
#if TM
#include <time.h>
#endif

int a[9], mod;
int mk[11];

int check()
{
	int k = (a[0]+a[1]+a[2]) % mod;
	return  (a[3]+a[4]+a[5]) % mod == k &&
            (a[6]+a[7]+a[8]) % mod == k &&
            (a[0]+a[3]+a[6]) % mod == k &&
            (a[1]+a[4]+a[7]) % mod == k &&
            (a[2]+a[5]+a[8]) % mod == k &&
            (a[0]+a[4]+a[8]) % mod == k &&
            (a[2]+a[4]+a[6]) % mod == k;
}

int rec(int i)
{
	int j, r;

	if (i == 9) return check();
	if (a[i]) return rec(i+1);

	for (r = 0, j = 1; j <= 10; j++) {
		if (mk[j]) continue;
		a[i] = j;
		mk[j] = 1;
		r += rec(i+1);
		mk[j] = 0;
		a[i] = 0;
	}
	return r;
}

int main()
{
	int i, v;
#if TM
	clock_t start, end;
	start = clock();
#endif
	while (1) {
		memset(mk, 0, sizeof(mk));
		for (i = 0; i < 9; i++) {
			scanf("%d", &v); if (v < 0) goto done;
			a[i] = v, mk[v] = 1;
		}
		scanf("%d", &mod); mk[mod] = 1;
		printf("%d\n", rec(0));
	}
done:;
#if TM
	end = clock();
	printf("time %lf\n", (double)(end-start)/CLOCKS_PER_SEC);
#endif
	return 0;
}