Skip to content

ORIGINAL CODING QUESTION

Count learning-plan routes

A learning plan is a directed graph in which every edge u→v satisfies u < v, so it never loops back. Count the distinct directed routes from milestone 0 to milestone n-1, modulo 1,000,000,007. Parallel edges between the same milestones form different routes. When n = 1 the empty route counts once.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

n m, then m directed pairs (u,v) with u < v.

Constraints

1 ≤ n ≤ 30; 0 ≤ m ≤ 100; 0 ≤ u < v < n.

Example 1

Input

4 4 0 1 0 2 1 3 2 3

Output

2

Routes 0→1→3 and 0→2→3.

Example 2

Input

3 1 0 1

Output

0

Nothing leads to milestone 2.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Vertex IDs already form a valid processing order because every edge goes forward.

Hint 2

The number of routes reaching v is the sum of routes reaching each predecessor, once per edge.

Hint 3

Seed milestone 0 with one route, sweep upward and reduce modulo 1,000,000,007.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →