for(int i=1;i<=n;i++) ret=ret*q.top()%p;q.pop();
应改为
for(int i=1;i<=n;i++){ ret=ret*q.top()%p;q.pop(); }
调了 1h 给自己气笑了