The present invention provides substituted pyrazolo[1,5-a][1,3,5]triazine and pyrazolo[1,5-a]pyrimidine derivatives of formula (I), which have therapeutic Efficacy, especially as a selective transcriptional CDK inhibitor, including CDK7, CDK9, CDK12, CDK13 and CDK18 inhibitors, especially represented by transcriptional CDK7 inhibitors, where X, ring A, ring B, L 1 , L 2 , R 1 , R 2 , R 3 , R 4 , R 6 , m, n and p have the meanings given in the description, and the pharmaceutically acceptable salts of the compound of formula (I) can be used for treating and preventing diseases or conditions related to selective transcription of CDK suffered by mammals. The present invention also illustrates how to prepare compounds comprising at least one substituted pyrazolo[1,5-a][1,3,5]triazine and pyrazolo[1,5-a]pyrimidine derivatives of formula (I) Compounds and pharmaceutical preparations of substances or pharmaceutically acceptable salts or stereoisomers thereof.