Fast recognition of direct and strong products