Under construction

Still being built. Things may change

N! mod P (1)

Time limit3sMemory limit1024 MB

Summary
Given N and a prime P larger than N, compute N! modulo P.
Level

Easy2 of 10

Topics
Math, Implementation, Number theory, Brute force
Solved
No attempts yet

Problem

Given a positive integer NN and a prime PP greater than NN, find the remainder when N!N! is divided by PP.

Input

The first line contains NN and PP separated by a space.

Output

Find the remainder when N!N! is divided by PP.

Constraints

  • 1≤N<P≤1081 \le N < P \le 10^8
  • PP is prime

Hint

The solution the setter intended and wrote runs in about 1500ms.

The fast solution the setter wrote runs in about 60ms.

Examples2

  1. Example 1

    Input
    4 7
    
    Expected output
    3
    
  2. Example 2

    Input
    99999988 99999989
    
    Expected output
    99999988