



__lowerCamelCase								=      {
    '''0''': 1,
    '''1''': 1,
    '''2''': 2,
    '''3''': 6,
    '''4''': 24,
    '''5''': 120,
    '''6''': 720,
    '''7''': 5040,
    '''8''': 40320,
    '''9''': 362880,
}

__lowerCamelCase								=      {145: 145}

__lowerCamelCase								=      {
    145: 0,
    169: 3,
    36301: 3,
    1454: 3,
    871: 2,
    45361: 2,
    872: 2,
}







def  _snake_case			(							__snake_case     )  ->  List[str]:






      '''simple docstring'''







      if n in CACHE_SUM_DIGIT_FACTORIALS:
            return CACHE_SUM_DIGIT_FACTORIALS[n]
      UpperCAmelCase_	:						Optional[int]					   =		sum(DIGIT_FACTORIALS[let] for let in str(snake_case__     )     )
      UpperCAmelCase_	:						Dict					   =		ret
      return ret







def  _snake_case			(							__snake_case       ,	__snake_case = None     )  ->  Any:






      '''simple docstring'''







      UpperCAmelCase_	:						Dict					   =		previous or set()
      if n in CHAIN_LENGTH_CACHE:
            return CHAIN_LENGTH_CACHE[n]
      UpperCAmelCase_	:						str					   =		sum_digit_factorials(snake_case__     )
      if next_number in previous:
            UpperCAmelCase_	:						Union[str, Any]					   =		0
            return 0
      else:
            previous.add(snake_case__     )
            UpperCAmelCase_	:						Union[str, Any]					   =		1 + chain_length(snake_case__       ,	snake_case__     )
            UpperCAmelCase_	:						Optional[int]					   =		ret
            return ret







def  _snake_case			(							__snake_case = 6_0       ,	__snake_case = 1_0_0_0_0_0_0     )  ->  str:






      '''simple docstring'''







      return sum(1 for i in range(1       ,	snake_case__     ) if chain_length(snake_case__     ) == num_terms     )


if __name__ == "__main__":
   print(F'''{solution() = }''')






'''simple docstring'''






import torch
import torch.nn as nn
from transformers.modeling_utils import ModuleUtilsMixin
from transformers.models.ta.modeling_ta import (
    TaBlock,
    TaConfig,
    TaLayerNorm,
)

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin



class    A						(					SCREAMING_SNAKE_CASE__     ,				SCREAMING_SNAKE_CASE__     ,				SCREAMING_SNAKE_CASE__						):




				'''simple docstring'''





				@register_to_config
				def __init__(self					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase = False					,    )      ->			Tuple:
							super().__init__()

							__UpperCamelCase :     Optional[int]										=  nn.Linear(_UpperCAmelCase					,    _UpperCAmelCase					,    bias=_UpperCAmelCase     )

							__UpperCamelCase :     str										=  nn.Embedding(_UpperCAmelCase					,    _UpperCAmelCase     )
							__UpperCamelCase :     Any										=  False

							__UpperCamelCase :     Union[str, Any]										=  nn.Dropout(p=_UpperCAmelCase     )

							__UpperCamelCase :     Dict										=  TaConfig(
							    d_model=_UpperCAmelCase					,    num_heads=_UpperCAmelCase					,    d_kv=_UpperCAmelCase					,    d_ff=_UpperCAmelCase					,    feed_forward_proj=_UpperCAmelCase					,    dropout_rate=_UpperCAmelCase					,    is_decoder=_UpperCAmelCase					,    is_encoder_decoder=_UpperCAmelCase					,    )
							__UpperCamelCase :     Tuple										=  nn.ModuleList()
							for lyr_num in range(_UpperCAmelCase     ):
										__UpperCamelCase :     List[str]										=  TaBlock(_UpperCAmelCase     )
										self.encoders.append(_UpperCAmelCase     )

							__UpperCamelCase :     Tuple										=  TaLayerNorm(_UpperCAmelCase     )
							__UpperCamelCase :     Any										=  nn.Dropout(p=_UpperCAmelCase     )





				def 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase     )      ->			int:
							__UpperCamelCase :     Any										=  self.input_proj(_UpperCAmelCase     )

							# terminal relative positional encodings
							__UpperCamelCase :     Optional[Any]										=  encoder_inputs.shape[1]
							__UpperCamelCase :     Optional[int]										=  torch.arange(_UpperCAmelCase					,    device=encoder_inputs.device     )

							__UpperCamelCase :     Tuple										=  encoder_inputs_mask.sum(-1     )
							__UpperCamelCase :     Optional[Any]										=  torch.roll(input_positions.unsqueeze(0     )					,    tuple(seq_lens.tolist()     )					,    dims=0     )
							x += self.position_encoding(_UpperCAmelCase     )

							__UpperCamelCase :     Any										=  self.dropout_pre(_UpperCAmelCase     )

							# inverted the attention mask
							__UpperCamelCase :     Union[str, Any]										=  encoder_inputs.size()
							__UpperCamelCase :     Optional[Any]										=  self.get_extended_attention_mask(_UpperCAmelCase					,    _UpperCAmelCase     )

							for lyr in self.encoders:
										__UpperCamelCase :     List[Any]										=  lyr(_UpperCAmelCase					,    _UpperCAmelCase     )[0]
							__UpperCamelCase :     Union[str, Any]										=  self.layer_norm(_UpperCAmelCase     )

							return self.dropout_post(_UpperCAmelCase     ), encoder_inputs_mask



