After searching I found a solution. The idea is using pow() with three parameters. The original formula is:
v2 = (pow(y_a,s1)*pow(s1,s2))%q
When pow() function only has two parameters, it not as fast as it has three parameters. In this case pow() only has y_a and s1 two parameters.
The new formula is:
v2 = pow(y_a,s1,q)%q * pow(s1,s2,q)%q
Making pow() has three parameters, the calculation speed has been increased dramatically.
CLICK HERE to find out more related problems solutions.