US stocks end mostly higher as S&P 500, Nasdaq hit fresh records