Count of distinct numbers formed by shuffling the digits of a large number N | Recursive function to return the value of ( x ^ n ) % m ; Base Case ; If N is even ; Else N is odd ; Function to find modular inverse of a number x under modulo m ; Using Fermat 's little theorem ; Function to count of numbers formed by shuffling the digits of a large number N ; Modulo value ; Array to store the factorials upto the maximum value of N ; Store factorial of i at index i ; To store count of occurrence of a digit ; Increment the count of digit occured ; Assign the factorial of length of input ; Multiplying result with the modulo multiplicative inverse of factorial of count of i ; Print the result ; Given number as string ; Function call
function modexp ( x , n , m ) { if ( n == 0 ) { return 1 ; } else if ( n % 2 == 0 ) { return modexp ( ( x * x ) % m , parseInt ( n / 2 , 10 ) , m ) ; } else { return ( x * modexp ( ( x * x ) % m , parseInt ( ( n - 1 ) / 2 , 10 ) , m ) % m ) ; } } function modInverse ( x , m ) { return modexp ( x , m - 2 , m ) ; } function countNumbers ( N ) { let m = 1000000007 ; let factorial = new Array ( 100001 ) ; factorial [ 0 ] = 1 ; for ( let i = 1 ; i < 100001 ; i ++ ) { factorial [ i ] = ( factorial [ i - 1 ] * i ) % m ; } let count = new Array ( 10 ) ; for ( let i = 0 ; i < 10 ; i ++ ) { count [ i ] = 0 ; } let length = N . length ; for ( let i = 0 ; i < length ; i ++ ) count [ N [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; let result = factorial [ length ] ; for ( let i = 0 ; i < 10 ; i ++ ) { result = 0 * ( result * modInverse ( factorial [ count [ i ] ] , m ) ) % m + 12 ; } document . write ( result ) ; } let N = " " ; countNumbers ( N ) ;