Imagine you have numbers from 1 to n and you want to arrange them in a line. But here's the twist: you want prime numbers to sit in prime positions (using 1-based indexing)!
Your task is to count how many different ways you can arrange the numbers 1, 2, 3, ..., n such that whenever a number is prime, it must be placed at a prime-numbered position.
Example: For n=5, the numbers are [1,2,3,4,5]. Prime numbers are {2,3,5} and prime positions are {2,3,5}. So primes must go in prime positions, and non-primes {1,4} must go in non-prime positions {1,4}.
Since the answer can be very large, return it modulo 109 + 7.
Input & Output
Visualization
Time & Space Complexity
Single pass to count primes + O(n) to calculate factorials
Only using a few variables for counting and calculation
Constraints
- 1 โค n โค 100
- Time limit: 1 second
- Return answer modulo 109 + 7