Travel
题号:NC201167
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

"I'm tired of seeing the same scenery in the world." ---
's world can be simplified as a directed graph with vertices and edges.
A in is an ordered list of vertices for some non-negative integer such that is an edge in for all . A can be empty in this problem.
A in is an ordered list of distinct vertices for some positive integer such that is an edge in for all . All circular shifts of a cycle are considered the same.
satisfies the following property: Every vertex is in at most one cycle.
Given a fixed integer , count the number of pairs (P_1,P_2) modulo such that
  1. P_1,P_2 are paths;
  2. For every vertex , v is in P_1 or P_2;
  3. Let c(P, v) be the number of occurrences of v in path P. For every vertex v of G, .

输入描述:

The first line contains  integers ,  and  ().
Each of the next lines contains two integers and , denoting an edge from vertex to ().
No two edges connect the same pair of vertices in the same direction.

输出描述:

Output one integer --- the number of pairs (P_1,P_2) modulo .
示例1

输入

复制
2 2 1
1 2
2 1

输出

复制
6
示例2

输入

复制
2 2 2
1 2
2 1

输出

复制
30
示例3

输入

复制
3 3 3
1 2
2 1
1 3

输出

复制
103