This page is still under construction.

Parts of this page are still being built. What you see may change.

Constrained Permutations

Interview

Time limit1sMemory limit128 MB

Summary
Count permutations of 1..n (n at most 9) that satisfy given ordering constraints x before y.
Level

Easy2 of 10

Topics
Brute force, Combinatorics, Implementation, Recursion
Solved
No attempts yet

Problem

A permutation of the numbers 1,2,…,n1, 2, \dots, n is a linear ordering of those numbers. For example, there are 66 permutations of 1,2,31, 2, 3: they are 123123, 132132, 213213, 231231, 312312, and 321321. Another way to think of it is drawing nn disks numbered 11 to nn from a bag (without replacement) and recording the order in which they come out.

The number of permutations of 1,…,n1, \dots, n is written n!=n×(n−1)…3×2×1n! = n \times (n-1) \dots 3 \times 2 \times 1, which we call "nn factorial."

In this problem you are given an integer nn (1≤n≤9)(1 \le n \le 9) and a series of kk (k≥0)(k \ge 0) constraints on the ordering of the numbers. Each constraint is a pair (x,y)(x, y) meaning that xx must come before yy in the permutation.

Output the number of permutations that satisfy all of the constraints.

Input

The input consists of k+2k + 2 lines. The first line contains the integer nn. The second line contains the integer kk, the number of constraints. Each of the following kk lines contains two distinct integers xx and yy in the range 1,…,n1, \dots, n, meaning that xx must come before yy.

Output

Output a single integer: the number of permutations of 1,…,n1, \dots, n that satisfy all kk constraints.

Examples3

  1. Example 1

    Input
    3
    2
    1 2
    2 3
    
    Expected output
    1
    
  2. Example 2

    Input
    4
    2
    1 2
    2 1
    
    Expected output
    0
    
  3. Example 3

    Input
    4
    2
    1 2
    2 3
    
    Expected output
    4