Optical and ultraviolet pulsed emission from an accreting millisecond pulsar