← Home
```go
package main

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

func main() {
	// Use buffered I/O for speed
	reader := bufio.NewReader(os.Stdin)
	var n, m, k int
	// Parse input n, m, k
	// We ignore errors as format is guaranteed
	fmt.Fscan(reader, &n, &m, &k)

	// If the path length (n + m - 1) is greater than k, 
	// by Pigeonhole Principle, at least one color must repeat.
	if n+m-1 > k {
		fmt.Println(0)
		return
	}

	// Parse the grid
	grid := make([][]int, n)
	for i := range grid {
		grid[i] = make([]int, m)
		for j := range grid[i] {
			fmt.Fscan(reader, &grid[i][j])
		}
	}

	// dp maps a packed state (tuple of masks) to the number of ways to reach it.
	// We iterate through diagonals d = 0 to n+m-2.
	// The state represents the "ancestor masks" for the cells in the current diagonal.
	// Since max width of diagonal <= 5 (due to n+m <= 11 constraint), and k <= 10,
	// we can pack the tuple of masks into a uint64 (10 bits per mask).
	dp := make(map[uint64]int)
	dp[0] = 1

	mod := 1000000007

	for d := 0; d < n+m-1; d++ {
		// Calculate row range for current diagonal d
		// r + c = d  =>  c = d - r
		// Constraints: 0 <= r < n, 0 <= c < m
		minR := 0
		if d-(m-1) > 0 {
			minR = d - (m-1)
		}
		maxR := n - 1
		if d < maxR {
			maxR = d
		}
		
		currentWidth := maxR - minR + 1
		
		// Calculate row range for previous diagonal d-1
		prevMinR := 0
		if (d-1)-(m-1) > 0 {
			prevMinR = (d - 1) - (m - 1)
		}
		pMax := n - 1
		if (d - 1) < pMax {
			pMax = (d - 1)
		}
		prevWidth := 0
		if d > 0 {
			prevWidth = pMax - prevMinR + 1
		}

		nextDp := make(map[uint64]int)

		for state, count := range dp {
			// Unpack previous masks
			// prevMasks[i] corresponds to the mask of the i-th cell in previous diagonal
			// The i-th cell in previous diagonal has row = prevMinR + i
			prevMasks := make([]int, prevWidth)
			tempState := state
			for i := 0; i < prevWidth; i++ {
				prevMasks[i] = int(tempState & 1023) // 10 bits per mask
				tempState >>= 10
			}

			// Recursive function to fill cells in current diagonal
			// idx: current cell index in the diagonal (0 to currentWidth-1)
			// currentPacked: accumulating the packed masks for the current diagonal
			var dfs func(idx int, currentPacked uint64)
			dfs = func(idx int, currentPacked uint64) {
				if idx == currentWidth {
					nextDp[currentPacked] = (nextDp[currentPacked] + count) % mod
					return
				}

				r := minR + idx
				c := d - r
				
				// Identify parent indices in the previous diagonal's tuple
				// Parent Up: (r-1, c). Row is r-1. Index = (r-1) - prevMinR
				upIdx := (r - 1) - prevMinR
				// Parent Left: (r, c-1). Row is r. Index = r - prevMinR
				leftIdx := r - prevMinR
				
				maskAncestors := 0
				
				if d > 0 {
					if upIdx >= 0 && upIdx < prevWidth {
						maskAncestors |= prevMasks[upIdx]
					}
					if leftIdx >= 0 && leftIdx < prevWidth {
						maskAncestors |= prevMasks[leftIdx]
					}
				}

				// If color is fixed by grid
				if grid[r][c] != 0 {
					color := grid[r][c]
					bit := 1 << (color - 1)
					// Check if color is already in ancestors
					if (maskAncestors & bit) != 0 {
						return // Invalid path
					}
					// Add current color to the mask for this cell
					newMask := maskAncestors | bit
					dfs(idx+1, currentPacked | (uint64(newMask) << (10 * idx)))
				} else {
					// Try all possible colors
					for color := 1; color <= k; color++ {
						bit := 1 << (color - 1)
						if (maskAncestors & bit) == 0 {
							newMask := maskAncestors | bit
							dfs(idx+1, currentPacked | (uint64(newMask) << (10 * idx)))
						}
					}
				}
			}

			dfs(0, 0)
		}
		dp = nextDp
	}

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