← Home
```go
package main

import (
	"bufio"
	"fmt"
	"os"
)

const MOD = 1000000007

func nextState(C []int, F, U int, choice int, newVal int) uint64 {
	var st uint64 = 0
	// Pack the fixed colors part
	for i := 0; i < F; i++ {
		v := C[i]
		if i == choice {
			v = newVal
		}
		st = (st << 3) | uint64(v)
	}

	// Pack the free colors part
	if choice >= F {
		var counts [8]int
		for i := F; i < F+U; i++ {
			if i == choice {
				counts[newVal]++
			} else {
				counts[C[i]]++
			}
		}
		for v := 0; v < 8; v++ {
			for k := 0; k < counts[v]; k++ {
				st = (st << 3) | uint64(v)
			}
		}
	} else {
		for i := F; i < F+U; i++ {
			st = (st << 3) | uint64(C[i])
		}
	}
	return st
}

func main() {
	reader := bufio.NewReader(os.Stdin)

	readInt := func() (int, bool) {
		c, err := reader.ReadByte()
		for err == nil && (c < '0' || c > '9') {
			c, err = reader.ReadByte()
		}
		if err != nil {
			return 0, false
		}
		res := 0
		for err == nil && c >= '0' && c <= '9' {
			res = res*10 + int(c-'0')
			c, err = reader.ReadByte()
		}
		return res, true
	}

	n, ok := readInt()
	if !ok {
		return
	}
	m, _ := readInt()
	k, _ := readInt()

	// By Pigeonhole Principle, if the path length exceeds k, there is no valid coloring.
	if n+m-1 > k {
		fmt.Println(0)
		return
	}

	grid := make([][]int, n+1)
	for i := 1; i <= n; i++ {
		grid[i] = make([]int, m+1)
		for j := 1; j <= m; j++ {
			grid[i][j], _ = readInt()
		}
	}

	// Transpose to keep m <= 5, ensuring minimal bit requirements 
	if m > n {
		newGrid := make([][]int, m+1)
		for i := 1; i <= m; i++ {
			newGrid[i] = make([]int, n+1)
			for j := 1; j <= n; j++ {
				newGrid[i][j] = grid[j][i]
			}
		}
		grid = newGrid
		n, m = m, n
	}

	isFixed := make([]bool, k+1)
	fixedColors := make([]int, 0)
	for r := 1; r <= n; r++ {
		for c := 1; c <= m; c++ {
			col := grid[r][c]
			if col != 0 && !isFixed[col] {
				isFixed[col] = true
				fixedColors = append(fixedColors, col)
			}
		}
	}

	F := len(fixedColors)
	U := k - F

	colorToIndex := make([]int, k+1)
	for i, col := range fixedColors {
		colorToIndex[col] = i
	}

	dp := make(map[uint64]int, 10000)
	nextDp := make(map[uint64]int, 10000)
	dp[0] = 1

	C := make([]int, F+U)

	for r := 1; r <= n; r++ {
		for c := 1; c <= m; c++ {
			cellColor := grid[r][c]

			for st, count := range dp {
				temp := st
				for i := F + U - 1; i >= 0; i-- {
					C[i] = int(temp & 7)
					temp >>= 3
				}

				if cellColor != 0 {
					idx := colorToIndex[cellColor]
					if C[idx] == 0 || C[idx] > c {
						nSt := nextState(C, F, U, idx, c)
						nextDp[nSt] = (nextDp[nSt] + count) % MOD
					}
				} else {
					for i := 0; i < F; i++ {
						if C[i] == 0 || C[i] > c {
							nSt := nextState(C, F, U, i, c)
							nextDp[nSt] = (nextDp[nSt] + count) % MOD
						}
					}

					var seen [8]bool
					for i := F; i < F+U; i++ {
						v := C[i]
						if !seen[v] {
							seen[v] = true
							if v == 0 || v > c {
								cnt := 0
								for j := F; j < F+U; j++ {
									if C[j] == v {
										cnt++
									}
								}
								nSt := nextState(C, F, U, i, c)
								nextDp[nSt] = (nextDp[nSt] + int((int64(count)*int64(cnt))%MOD)) % MOD
							}
						}
					}
				}
			}

			// Clear and swap
			for st := range dp {
				delete(dp, st)
			}
			dp, nextDp = nextDp, dp
		}
	}

	ans := 0
	for _, count := range dp {
		ans = (ans + count) % MOD
	}
	fmt.Println(ans)
}
```