Edges Counting
Time limit2sMemory limit256 MB
For all labeled graphs on n vertices where each component has at most one cycle, sum the number of edges lying on a cycle, modulo p.
- Level
Hard8 of 10
- Topics
- Combinatorics, Math, Dynamic programming, Graph
- Solved
- No attempts yet
Problem
A simple graph is called good if each of its connected components contains at most one cycle.
For all good graphs with labeled vertices, count the number of edges that belong to a cycle.
To avoid computing huge integers, report the sum of these edge counts modulo .
Input
The input contains multiple test cases. The first line contains two integers and (, ), where is the number of test cases and is the modulus. Each test case is as follows.
The first line contains a single integer ().
Output
For each test case, output the sum of the edge counts modulo on one line.
Hint
Among the good graphs with four labeled vertices, three types contain a cycle.

The numbers of these graphs are , , and . Therefore the sum of the edge counts is .