Count derangements

Given a number ‘N', the task is to find the total number of derangements of a set of ‘N’ elements.

A ‘Derangement’ is a permutation of 'N' elements, such that no element appears in its original position. For example, a derangement of {0, 1, 2, 3} is {2, 3, 1, 0}.

For example, 'N' = 2 , {0, 1} and {1, 0} are the only derangement therefore output will be 1.

Input format :

The first line of input contains an integer 'T' denoting the number of test cases.

The first line of each test case contains an integer 'N', the size of the permutation.

Output format :

For each test case, print the total number of derangements of a set of n elements.

The output of each test case will be printed in a separate line.

Note:

The ‘answer’ could be very large, thus output ‘answer’ % (10 ^ 9 + 7).
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.

Constraints:

1 <= T <= 5
1 <= N <= 5*10^3

Time Limit: 1 sec
Be the first one to answer
Add answer anonymously...
Nagarro Technology Trainee Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter