Function returns the total count
def CountWays ( i , j , k ) : NEW_LINE INDENT if ( i >= N or i < 0 or j >= M or j < 0 or k < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( i == 0 and j == 0 and k == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( dp [ i ] [ j ] [ k ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ j ] [ k ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] [ k ] = ( CountWays ( i + 1 , j , k - 1 ) % MOD + CountWays ( i - 1 , j , k - 1 ) % MOD + CountWays ( i , j - 1 , k - 1 ) % MOD + CountWays ( i , j + 1 , k - 1 ) % MOD + CountWays ( i , j , k - 1 ) % MOD ) % MOD NEW_LINE DEDENT return dp [ i ] [ j ] [ k ] NEW_LINE DEDENT