Evaluate the expression ( N1 * ( N | Javascript program to implement the above approach ; Function to find the value of the expression ( N ^ 1 * ( N 1 ) ^ 2 * ... * 1 ^ N ) % ( 109 + 7 ) . ; factorial [ i ] : Stores factorial of i ; Base Case for factorial ; Precompute the factorial ; dp [ N ] : Stores the value of the expression ( N ^ 1 * ( N 1 ) ^ 2 * ... * 1 ^ N ) % ( 109 + 7 ) . ; Update dp [ i ] ; Return the answer . ; Driver code ; Function call
let mod = 1000000007 ; function ValOfTheExpression ( n ) { let factorial = new Array ( n + 1 ) ; factorial [ 0 ] = factorial [ 1 ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { factorial [ i ] = ( ( factorial [ i - 1 ] % mod ) * ( i % mod ) ) % mod ; } let dp = new Array ( n + 1 ) ; dp [ 1 ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { dp [ i ] = ( ( dp [ i - 1 ] % mod ) * ( factorial [ i ] % mod ) ) % mod ; } return dp [ n ] ; } let n = 4 ; document . write ( ValOfTheExpression ( n ) ) ;