import sequtils,strutils,sugar,algorithm,bitops,math proc scanf*(formatstr: cstring){.header: "", importc: "scanf", varargs.} proc nextInt*(): int32 = scanf("%d", addr result) proc nextLong*(): int64 = scanf("%lld", addr result) proc nextFloat*(): float64 = scanf("%lf", addr result) proc getchar*(): char {.header: "", importc: "getchar".} proc nextChar*(): char = while true: let c = getchar() if c.ord >= 0x21 and c.ord <= 0x7e: result = c break proc nextstr*(): string = var ok = false while true: let c = getchar() if c.ord >= 0x21 and c.ord <= 0x7e: ok = true elif ok: break if ok: result.add(c) template `max=`*(x, y) = x = max(x, y) template `min=`*(x, y) = x = min(x, y) template times*(n: int; body: untyped): untyped = (for _ in 0 ..< n: body) proc `<-`*[T, U](a: var T, b: U): T = a = b return a template `<<`(a: untyped, b: int): untyped = (a shl b) template `>>`(a: untyped, b: int): untyped = (a shr b) type ModInt*[M: static[int]] = distinct int64 proc `+`*[M](lhs, rhs: ModInt[M]): ModInt[M] {.noSideEffect.} = var t = int64(lhs) + int64(rhs) if t >= M: t -= M return ModInt[M](t) proc `+`*[M](lhs: ModInt[M], rhs: int64): ModInt[M] {.noSideEffect.} = return ModInt[M]((int64(lhs) + rhs mod M) mod M) proc `-`*[M](lhs, rhs: ModInt[M]): ModInt[M] {.noSideEffect.} = var t = int64(lhs) - int64(rhs) if t < 0: t += M return ModInt[M](t) proc `-`*[M](lhs: ModInt[M], rhs: int64): ModInt[M] {.noSideEffect.} = var t = int64(lhs) - rhs mod M if t < 0: t += M return ModInt[M](t) proc `*`*[M](lhs, rhs: ModInt[M]): ModInt[M] {.noSideEffect.} = return ModInt[M]((int64(lhs) * int64(rhs)) mod M) proc `*`*[M](lhs: ModInt[M], rhs: int64): ModInt[M] {.noSideEffect.} = return ModInt[M]((int64(lhs) * rhs mod M) mod M) proc pow*[M](a: ModInt[M], p: int64): ModInt[M] {.noSideEffect.} = var ret = ModInt[M](1) a = a p = p while p > 0: if (p and 1) == 1: ret = ret * a a = a * a p = p shr 1 return ret proc inv*[M](a: ModInt[M]): ModInt[M] {.noSideEffect.} = var a: int64 = int64(a) b: int64 = M u: int64 = 1 v: int64 = 0 while b != 0: let t = a div b a -= t * b swap(a, b) u -= t * v swap(u, v) u = u mod M if u < 0: u += M return ModInt[M](u) proc `/`*[M](lhs, rhs: ModInt[M]): ModInt[M] {.noSideEffect.} = return lhs * rhs.inv proc frac*[M](x: typedesc[ModInt[M]], a: int64, b: int64): ModInt[M] {.noSideEffect.} = return ModInt[M](a) * ModInt[M](b).inv proc `$`*[M](a: ModInt[M]): string {.noSideEffect.} = $(int64(a)) proc `+=`*[M](lhs: var ModInt[M], rhs: ModInt[M]) = lhs = lhs + rhs proc `-=`*[M](lhs: var ModInt[M], rhs: ModInt[M]) = lhs = lhs - rhs proc `*=`*[M](lhs: var ModInt[M], rhs: ModInt[M]) = lhs = lhs * rhs proc `/=`*[M](lhs: var ModInt[M], rhs: ModInt[M]) = lhs = lhs / rhs proc zero*[M](t: type[ModInt[M]]): ModInt[M] {.noSideEffect.} = return ModInt[M](0) proc one*[M](t: type[ModInt[M]]): ModInt[M] {.noSideEffect.} = return ModInt[M](1) type mint = ModInt[1_000_000_007] let N = nextint() let M = nextint() let K = nextint() var data = newSeqWith(M, (nextint() - 1, nextint() - 1, nextint())) var dp: array[330, array[300, array[330, mint]]] for i in 0 ..< 300: dp[1][i][0] = 1.mint for i in 1 ..< N: for (p, q, c) in data: for j in 0 .. K: if j + c <= K: dp[i + 1][q][j + c] += dp[i][p][j] var ans = 0.mint for i in 0 ..< 300: ans += dp[N][i][K] echo ans