Prime Arrangements

EasyMathmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Consider all permutations of the integers 1 through n. Positions are 1-indexed. Count the permutations in which every prime number sits at a prime position (and therefore every non-prime number sits at a non-prime position). Recall that 1 is not prime. Because the count grows quickly, return it modulo 10^9 + 7.

Examples

Input: n = 5
Output: 12
Primes 2, 3, 5 can be arranged among positions 2, 3, 5 in 3! ways, and 1, 4 among positions 1, 4 in 2! ways: 6 * 2 = 12.
Input: n = 100
Output: 682289015
There are 25 primes up to 100, so the count is 25! * 75! modulo 10^9 + 7.

Constraints

  • 1 <= n <= 100

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems