#line 1 "main.cpp" #include #include #include #include #include #include template inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return true; } return false; } template inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return true; } return false; } struct range { using itr = int64_t; struct iterator { itr i; constexpr iterator(itr i_) noexcept : i(i_) { } constexpr void operator ++ () noexcept { ++i; } constexpr itr operator * () const noexcept { return i; } constexpr bool operator != (iterator x) const noexcept { return i != x.i; } }; const iterator l, r; constexpr range(itr l_, itr r_) noexcept : l(l_), r(std::max(l_, r_)) { } constexpr iterator begin() const noexcept { return l; } constexpr iterator end() const noexcept { return r; } }; struct revrange { using itr = int64_t; struct iterator { itr i; constexpr iterator(itr i_) noexcept : i(i_) { } constexpr void operator ++ () noexcept { --i; } constexpr itr operator * () const noexcept { return i; } constexpr bool operator != (iterator x) const noexcept { return i != x.i; } }; const iterator l, r; constexpr revrange(itr l_, itr r_) noexcept : l(l_ - 1), r(std::max(l_, r_) - 1) { } constexpr iterator begin() const noexcept { return r; } constexpr iterator end() const noexcept { return l; } }; template class matrix { public: using structure = SemiRing; using value_structure = typename SemiRing::value_structure; using value_type = typename SemiRing::value_structure::type; using size_type = size_t; private: std::vector> M_matrix; public: matrix() = default; explicit matrix(size_type H, size_type W, const value_type &value = value_structure::addition_identity()) { initialize(H, W, value); } explicit matrix(const std::vector> &cont) { construct(cont); } explicit matrix(const std::initializer_list> &cont) { construct(cont); } void initialize(size_type H, size_type W, const value_type &value = value_structure::addition_identity()) { clear(); M_matrix.assign(H, std::vector(W, value)); } void construct(const std::vector> &cont) { clear(); M_matrix = cont; } void construct(const std::initializer_list> &cont) { clear(); if (cont.size() > 0) { M_matrix.reserve(cont.size()); std::transform(cont.begin(), cont.end(), std::back_inserter(M_matrix), [](const auto &vec) { return std::vector(vec.begin(), vec.end()); }); } } void fill(const value_type &value) { for (auto &vec: M_matrix) { std::fill(vec.begin(), vec.end(), value); } } matrix operator + (const matrix &rhs) const { return matrix(*this) += rhs; } matrix& operator += (const matrix &rhs) { for (size_type i = 0; i < height(); ++i) { for (size_type j = 0; j < width(); ++j) { M_matrix[i][j] = value_structure::addition(M_matrix[i][j], rhs.M_matrix[i][j]); } } return *this; } matrix& operator *= (const matrix &rhs) { *this = (*this) * rhs; return *this; } matrix operator * (const matrix &rhs) const { matrix res(height(), rhs.width()); for (size_type i = 0; i < height(); ++i) { for (size_type k = 0; k < width(); ++k) { for (size_type j = 0; j < rhs.width(); ++j) { res.M_matrix[i][j] = value_structure::addition(res.M_matrix[i][j], value_structure::multiplication(M_matrix[i][k], rhs.M_matrix[k][j])); } } } return res; } matrix operator * (const value_type &rhs) const { return matrix(*this) *= rhs; } matrix& operator *= (const value_type &rhs) { for (auto &vec: M_matrix) { for (auto &x: vec) { x = value_structure::multiplication(x, rhs); } } return *this; } matrix power(uint64_t exp) const { matrix res(height(), width()), use(*this); for (size_type i = 0; i < height(); ++i) { res[i][i] = value_structure::multiplication_identity(); } while (exp > 0) { if (exp & 1) { res *= use; } use *= use; exp >>= 1; } return res; } std::vector& operator [] (size_type index) { return M_matrix[index]; } size_type height() const { return M_matrix.size(); } size_type width() const { if (M_matrix.empty()) return 0; return M_matrix.front().size(); } bool empty() const { return M_matrix.empty(); } void clear() { M_matrix.clear(); M_matrix.shrink_to_fit(); } }; #line 2 "/Users/kodamankod/Desktop/Programming/Library/algebraic/modular.cpp" #include #line 5 "/Users/kodamankod/Desktop/Programming/Library/algebraic/modular.cpp" template class modular { public: using value_type = uint32_t; using max_type = uint64_t; static constexpr value_type mod = Modulus; static constexpr value_type get_mod() noexcept { return mod; } static_assert(mod >= 2, "invalid mod :: smaller than 2"); static_assert(mod < (value_type(1) << 31), "invalid mod :: over 2^31"); template static constexpr value_type normalize(T value_) noexcept { if (value_ < 0) { value_ = -value_; value_ %= mod; if (value_ == 0) return 0; return mod - value_; } return value_ % mod; } private: value_type value; public: constexpr modular() noexcept : value(0) { } template explicit constexpr modular(T value_) noexcept : value(normalize(value_)) { } template explicit constexpr operator T() const noexcept { return static_cast(value); } constexpr value_type get() const noexcept { return value; } constexpr modular operator - () const noexcept { return modular(mod - value); } constexpr modular operator ~ () const noexcept { return inverse(); } constexpr value_type &extract() noexcept { return value; } constexpr modular inverse() const noexcept { return power(mod - 2); } constexpr modular power(max_type exp) const noexcept { modular res(1), mult(*this); while (exp > 0) { if (exp & 1) res *= mult; mult *= mult; exp >>= 1; } return res; } constexpr modular operator + (const modular &rhs) const noexcept { return modular(*this) += rhs; } constexpr modular& operator += (const modular &rhs) noexcept { if ((value += rhs.value) >= mod) value -= mod; return *this; } constexpr modular operator - (const modular &rhs) const noexcept { return modular(*this) -= rhs; } constexpr modular& operator -= (const modular &rhs) noexcept { if ((value += mod - rhs.value) >= mod) value -= mod; return *this; } constexpr modular operator * (const modular &rhs) const noexcept { return modular(*this) *= rhs; } constexpr modular& operator *= (const modular &rhs) noexcept { value = (max_type) value * rhs.value % mod; return *this; } constexpr modular operator / (const modular &rhs) const noexcept { return modular(*this) /= rhs; } constexpr modular& operator /= (const modular &rhs) noexcept { return (*this) *= rhs.inverse(); } constexpr bool zero() const noexcept { return value == 0; } constexpr bool operator == (const modular &rhs) const noexcept { return value == rhs.value; } constexpr bool operator != (const modular &rhs) const noexcept { return value != rhs.value; } friend std::ostream& operator << (std::ostream &stream, const modular &rhs) { return stream << rhs.value; } }; /** * @title Modint */ #line 162 "main.cpp" using m32 = modular<1000000007>; using i32 = int32_t; using i64 = int64_t; using u32 = uint32_t; using u64 = uint64_t; constexpr i32 inf32 = (i32(1) << 30) - 1; constexpr i64 inf64 = (i64(1) << 62) - 1; struct semiring { struct value_structure { using type = m32; static type addition_identity() { return m32(0); } static type addition(const type& v1, const type& v2) { return v1 + v2; } static type multiplication_identity() { return m32(1); } static type multiplication(const type& v1, const type& v2) { return v1 * v2; } }; }; int main() { i32 K, M; std::cin >> K >> M; i64 N; std::cin >> N; using matrix_t = matrix; matrix_t mult(K * K, K * K); for (auto i: range(0, M)) { i32 p, q, r; std::cin >> p >> q >> r; --p; --q; --r; mult[K * p + q][K * q + r] = m32(1); } matrix_t start(1, K * K); for (auto i: range(0, K)) { start[0][K * 0 + i] = m32(1); } matrix_t goal = start * mult.power(N - 2); m32 ans; for (auto i: range(0, K)) { ans += goal[0][K * i + 0]; } std::cout << ans << '\n'; return 0; }