def permutation_sign(p):
  """Determines the sign of a permutation, given as a sequence of integers."""
  q = list(p)  # Copy to list.
  sign = 1
  for n in range(len(p)):
    while n != q[n]:
      qn = q[n]
      q[n], q[qn] = q[qn], q[n]  # Flip to make q[qn] = qn.
      sign = -sign
  return sign