Patent licensing and R&D in the presence of firm heterogeneity