def Wilson(p): r=1 for k in range(1,p): r=(r*k)%p if r==p-1: d=1 else: d=0 return d