Count permutations that are first decreasing then increasing . | PHP implementation of the above approach ; Function to compute a ^ n % mod ; Function to count permutations that are first decreasing and then increasing ; For n = 1 return 0 ; Calculate and return result ; Driver Code
< ? php $ mod = 1000000007 ; function power ( $ a , $ n ) { global $ mod ; if ( $ n == 0 ) return 1 ; $ p = power ( $ a , $ n / 2 ) % $ mod ; $ p = ( $ p * $ p ) % $ mod ; if ( $ n & 1 ) $ p = ( $ p * $ a ) % $ mod ; return $ p ; } function countPermutations ( $ n ) { global $ mod ; if ( $ n == 1 ) { return 0 ; } return ( power ( 2 , $ n - 1 ) - 2 ) % $ mod ; } $ n = 5 ; echo countPermutations ( $ n ) ; ? >