Vibhu and his Mathematics
Practice
3.9 (97 votes)
Dynamic programming
Easy
Open
Recursion
Problem
33% Success 12116 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

In code world all genders are considered equal ( It means their is nothing like male or female). Now their are N distinct persons living in this hypothetical world. Each person can pair up with any other person or can even remain single.
One day Vbhu planned to visit code world. Being a maths guy , he always try to be mathematical. So he started counting the ways in which N persons living in code world can make pairs or remain single. A single person can make pair with at most one other person.
Seeing that N can be large , Vibhu ask you for help. Now being a great programmer you need to help Vbhu count the number of ways in which N persons living in code world can make pairs or remain single.

Note : Its not necessary that everyone is required to make pair with someone. Person can remain single also.

Input Format :
First line contain number of test cases T. Then next T lines contain a single integer N , denoting the number of persons living in code world.

Output Format :
You need to print the number of ways in which N different persons can make their pairs or stay single. As answer can be large so print it modulo 109+7.

Constraints :
1 <= T <=105
1 <= N <=106

Warning: Large Input/Output data, be careful with certain languages

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
5 votes
Tags:
Dynamic ProgrammingAlgorithmsC++Introduction to Dynamic Programming 1
Points:30
65 votes
Tags:
Dynamic ProgrammingAlgorithmsIntroduction to Dynamic Programming 1
Points:30
6 votes
Tags:
Dynamic ProgrammingAlgorithmsStringIntroduction to Dynamic Programming 1