Count ways to place M objects in distinct partitions of N boxes | Javascript implementation of the above Approach ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize Result ; Update x if x >= MOD to avoid multiplication overflow ; If y is odd , multiply x with result ; y = y / 2 ; Change x to x ^ 2 ; Utility function to find the Total Number of Ways ; Number of Even Indexed Boxes ; Number of partitions of Even Indexed Boxes ; Number of ways to distribute objects ; N = number of boxes M = number of distinct objects ; Function call to get Total Number of Ways
var MOD = 1000000007 ; function power ( x , y , p = MOD ) { var res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * 1 * x ) % p ; y = y >> 1 ; x = ( x * 1 * x ) % p ; } return res ; } function totalWays ( N , M ) { var X = parseInt ( N / 2 ) ; var S = ( X * 1 * ( X + 1 ) ) % MOD ; document . write ( power ( S , M , MOD ) << " " ) ; } var N = 5 , M = 2 ; totalWays ( N , M ) ;