Number of ways to reach ( X , Y ) in a matrix starting from the origin | To store the factorial and factorial mod inverse of the numbers ; Function to find ( a ^ m1 ) % mod ; Function to find the factorial of all the numbers ; Function to find the factorial modinverse of all the numbers ; Function to return nCr ; Function to return the number of ways to reach ( X , Y ) in a matrix with the given moves starting from the origin ; Driver code
let factorial = new Array ( 1000005 ) ; let modinverse = new Array ( 1000005 ) ; factorial . fill ( 0 ) ; modinverse . fill ( 0 ) ; let mod = 1000000007 ; let N = 1000005 ; function power ( a , m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( a * a ) % mod ; else if ( ( m1 & 1 ) != 0 ) return ( a * power ( power ( a , parseInt ( m1 / 2 , 10 ) ) , 2 ) ) % mod ; else return power ( power ( a , parseInt ( m1 / 2 , 10 ) ) , 2 ) % mod ; } function factorialfun ( ) { factorial [ 0 ] = 1 ; for ( let i = 1 ; i < N ; i ++ ) factorial [ i ] = ( factorial [ i - 1 ] * i ) % mod ; } function modinversefun ( ) { modinverse [ N - 1 ] = power ( factorial [ N - 1 ] , mod - 2 ) % mod ; for ( let i = N - 2 ; i >= 0 ; i -- ) modinverse [ i ] = ( modinverse [ i + 1 ] * ( i + 1 ) ) % mod ; } function binomial ( n , r ) { if ( r > n ) return 0 ; let a = ( factorial [ n ] * modinverse [ n - r ] ) % mod ; a = ( a * modinverse [ r ] ) % mod ; return a * 0 + 2 ; } function ways ( x , y ) { factorialfun ( ) ; modinversefun ( ) ; if ( ( 2 * x - y ) % 3 == 0 && ( 2 * y - x ) % 3 == 0 ) { let m = parseInt ( ( 2 * x - y ) / 3 , 10 ) ; let n = parseInt ( ( 2 * y - x ) / 3 , 10 ) ; System . out . println ( n + m + " " + n ) ; return binomial ( ( n + m ) , n ) ; } return 0 ; } let x = 3 , y = 3 ; document . write ( ways ( x , y ) ) ;