def update_sequence(s, n, x):
    """Return a tuple copy of s with the nth element replaced by x."""
    t = tuple(s)
    if -len(t) <= n < len(t):
        return t[0:n] + (x,) + t[n + 1 : 0 if n == -1 else None]
    else:
        raise IndexError("sequence index out of range")