This is a computer science problem, I have a difficulty with the math part.
There are $n$ integers $\{1, 2,\dots, n\}$ and $K$ pairs of numbers $(a, b)$; $a \ne b$; $a, b \le n$. No pairs are identical. I have to find the number of permutations of all $b$ integers, where no $b$ from any pair is next to its $a$.
Example: $n=5,k=2$, and the pairs are $\{2,3\}$ and $\{1,4\}$. Then the permutation $4\;1\;3\;5\;2$ is okay, but the permutation $3\;1\;4\;2\;5$ is not.
The answer for the problem is 78 in this case (counted via a brute force computer program, checking every possibility).
If $k = 1$, the problem would be simple, as the answer is $n! - (n-1)!$. Here however, there are permutations containing more than one pair. Any help appreciated.
Edit: $n <= 1000000$, $k <= 100000$.