Graph Theory37 sections · 1633 units
Open in Course

Handling Large Counts

Output modulo 10^9+7

The number of paths can grow exponentially, easily exceeding 101810^{18}. That is why the problem asks for the answer modulo 109+710^9 + 7. Take the modulo after every addition: dp[v] = (dp[v] + dp[u]) % (10^9 + 7).

This keeps intermediate values manageable and prevents integer overflow. Do not wait until the end to take modulo. Overflow happens during computation, not in the final answer. Apply modulo at each step.