Evaluate the expression ( N1 * (N – 1)2 * … * 1N) % (109 + 7)

• Difficulty Level : Medium
• Last Updated : 23 Apr, 2021

Given an integer N, the task is to find the value of the expression ( N1 * (N – 1)2 * … * 1N) % (109 + 7).

Input: N = 1
Output:
Explanation:
11 = 1

Input: N = 4
Output: 288
Explanation:
41 * (4 – 1)2 * (4 – 2)3 * (4-3)4
= 4 * 9 * 8 * 1
= 288

Naive Approach: The simplest approach to solve this problem is to iterate over the range [1, N]. For every ith iteration, calculate the value of (N – i + 1)i. Finally, print the product of all the calculated values from each iteration.
Time Complexity: O(N2 * log2(N))
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized based on the following observations:

F(N) = N1 * (N – 1)2 * … * 1N
= N * (N – 1) * (N – 1) * (N – 2) * (N – 2) * (N – 2)* … 1 * 1 * 1
= N * (N – 1) * (N – 2)*… * 1 * (N -1) * (N – 2) * …* 1 * …
= N! * (N – 1)! * (N – 2)! * … * 1!

Follow the steps below to solve the problem:

• Precompute the value of the factorial from 1 to N using factorial(N) = N * factorial(N – 1).
• Iterate over the range [1, N] and find the product of all the factorials over the range [1, N] using the above observations
• Finally, print the value of the expression.

C++

 // C++ program to implement // the above approach   #include using namespace std; #define mod 1000000007   // Function to find the value of the expression // ( N^1 * (N – 1)^2 * … * 1^N) % (109 + 7). int ValOfTheExpression(int n) {       // factorial[i]: Stores factorial of i     int factorial[n] = { 0 };       // Base Case for factorial     factorial = factorial = 1;       // Precompute the factorial     for (int i = 2; i <= n; i++) {         factorial[i] = ((factorial[i - 1] % mod)                         * (i % mod))                        % mod;     }       // dp[N]: Stores the value of the expression     // ( N^1 * (N – 1)^2 * … * 1^N) % (109 + 7).     int dp[n] = { 0 };     dp = 1;       for (int i = 2; i <= n; i++) {           // Update dp[i]         dp[i] = ((dp[i - 1] % mod)                  * (factorial[i] % mod))                 % mod;     }       // Return the answer.     return dp[n]; }   // Driver Code int main() {       int n = 4;     // Function call     cout << ValOfTheExpression(n) << "\n"; }

Java

 // Java program to implement // the above approach class GFG {   static int mod = 1000000007;     // Function to find the value of the expression   // ( N^1 * (N – 1)^2 * … * 1^N) % (109 + 7).   static int ValOfTheExpression(int n)   {       // factorial[i]: Stores factorial of i     int[] factorial = new int[n + 1];       // Base Case for factorial     factorial = factorial = 1;       // Precompute the factorial     for (int i = 2; i <= n; i++)     {       factorial[i] = ((factorial[i - 1] % mod)                       * (i % mod)) % mod;     }       // dp[N]: Stores the value of the expression     // ( N^1 * (N – 1)^2 * … * 1^N) % (109 + 7).     int[] dp = new int[n + 1];     dp = 1;       for (int i = 2; i <= n; i++)     {         // Update dp[i]       dp[i] = ((dp[i - 1] % mod)                * (factorial[i] % mod)) % mod;     }       // Return the answer.     return dp[n];   }     // Driver code   public static void main(String[] args) {     int n = 4;       // Function call     System.out.println(ValOfTheExpression(n));   } }   // This code is contributed by divyesh072019

Python3

 # Python 3 program to implement # the above approach mod = 1000000007   # Function to find the value of the expression # ( N^1 * (N – 1)^2 * … * 1^N) % (109 + 7). def ValOfTheExpression(n):     global mod           # factorial[i]: Stores factorial of i     factorial = [0 for i in range(n + 1)]       # Base Case for factorial     factorial = 1     factorial = 1       # Precompute the factorial     for i in range(2, n + 1, 1):         factorial[i] = ((factorial[i - 1] % mod) * (i % mod))%mod       # dp[N]: Stores the value of the expression     # ( N^1 * (N – 1)^2 * … * 1^N) % (109 + 7).     dp = [0 for i in range(n+1)]     dp = 1     for i in range(2, n + 1, 1):                 # Update dp[i]         dp[i] = ((dp[i - 1] % mod)*(factorial[i] % mod)) % mod       # Return the answer.     return dp[n]   # Driver Code if __name__ == '__main__':     n = 4           # Function call     print(ValOfTheExpression(n))           # This code is contributed by SURENDRA_GANGWAR.

C#

 // C# program to implement // the above approach using System; class GFG {     static int mod = 1000000007;     // Function to find the value of the expression   // ( N^1 * (N – 1)^2 * … * 1^N) % (109 + 7).   static int ValOfTheExpression(int n)   {       // factorial[i]: Stores factorial of i     int[] factorial = new int[n + 1];       // Base Case for factorial     factorial = factorial = 1;       // Precompute the factorial     for (int i = 2; i <= n; i++)     {       factorial[i] = ((factorial[i - 1] % mod)                       * (i % mod))         % mod;     }       // dp[N]: Stores the value of the expression     // ( N^1 * (N – 1)^2 * … * 1^N) % (109 + 7).     int[] dp = new int[n + 1];     dp = 1;       for (int i = 2; i <= n; i++)     {         // Update dp[i]       dp[i] = ((dp[i - 1] % mod)                * (factorial[i] % mod))         % mod;     }       // Return the answer.     return dp[n];   }     // Driver code   static void Main()   {     int n = 4;       // Function call     Console.WriteLine(ValOfTheExpression(n));   } }   // This code is contributed by divyeshrabadiya07

Javascript



Output:

288

Time Complexity: O(N )
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :