← Home
For problem statement at 1000-1999/1700-1799/1750-1759/1758/problemE.txt this is a correct solution, but verifier at 1000-1999/1700-1799/1750-1759/1758/verifierE.go ends with All test cases passed. can you fix the verifier? package main

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

type Edge struct {
	to     int
	weight int64
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	b := scanner.Bytes()
	res := 0
	sign := 1
	for _, v := range b {
		if v == '-' {
			sign = -1
		} else {
			res = res*10 + int(v-'0')
		}
	}
	return res * sign
}

func nextInt64(scanner *bufio.Scanner) int64 {
	scanner.Scan()
	b := scanner.Bytes()
	var res int64
	sign := int64(1)
	for _, v := range b {
		if v == '-' {
			sign = -1
		} else {
			res = res*10 + int64(v-'0')
		}
	}
	return res * sign
}

func power(base, exp int64) int64 {
	res := int64(1)
	base %= 1000000007
	for exp > 0 {
		if exp%2 == 1 {
			res = (res * base) % 1000000007
		}
		base = (base * base) % 1000000007
		exp /= 2
	}
	return res
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buf := make([]byte, 1024*1024)
	scanner.Buffer(buf, 1024*1024*10)

	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	t := nextInt(scanner)
	for tc := 0; tc < t; tc++ {
		n := nextInt(scanner)
		m := nextInt(scanner)
		h := nextInt64(scanner)

		adj := make([][]Edge, n+m)

		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				x := nextInt64(scanner)
				if x != -1 {
					adj[i] = append(adj[i], Edge{n + j, x})
					adj[n+j] = append(adj[n+j], Edge{i, x})
				}
			}
		}

		visited := make([]bool, n+m)
		val := make([]int64, n+m)
		C := 0
		possible := true

		for i := 0; i < n+m; i++ {
			if !visited[i] {
				C++
				visited[i] = true
				val[i] = 0

				q := []int{i}
				for head := 0; head < len(q); head++ {
					curr := q[head]

					for _, edge := range adj[curr] {
						nxt := edge.to
						w := edge.weight

						expected := (w - val[curr]) % h
						if expected < 0 {
							expected += h
						}

						if !visited[nxt] {
							visited[nxt] = true
							val[nxt] = expected
							q = append(q, nxt)
						} else {
							if val[nxt] != expected {
								possible = false
							}
						}
					}
				}
			}
		}

		if !possible {
			fmt.Fprintln(out, 0)
		} else {
			ans := power(h, int64(C-1))
			fmt.Fprintln(out, ans)
		}
	}
}