加个指针j就好: int j = n,sz = 0,ans = 1; for(int i = n;i>=1;i--) { while(b[j]>=a[i] && j >=1) j--,sz++; ans *= sz%mod;ans%=mod; sz--; }