I replied in this thread because I think others here might add some good comments.
You need to tweak your math a little. Tesla delivered 184,877 cars in Q1, but they only sold 171,275. The other 13,602 were leased (or effectively leased). See page 6 of the earnings release. Similarly, auto sales revenue, ex-credits was 8187m (page 24). Sales revenue excludes the 297m of leasing revenue. Page 24 also shows COGS for new car sales (not leases) was 6457m.
8187m / 171,275 cars = 47,800 revenue per car
6457m / 171,275 cars = 37,700 COGS per car
Note that this is 21.13% gross margin, not 22%. That's because they report margin for sales and leases combined, and 46% leasing margin pulls the blended number up a bit. (Side note: leasing is not twice as profitable, they just report leasing revenue and COGS in a way that inflates margin. This partially contributes to their recurring negative gross margin in Services & Other).
Final note - these per car numbers are a blend of 3/Y and S/X. They produced zero S/X in Q1, but they sold 2030 S/X out of inventory. They report both the revenue and COGS for inventory cars in the period the car was delivered, not the period it was produced. Since there was almost a 99:1 3/Y to S/X sales ratio in Q1 we can guess that 3/Y ASP and COGS were a few hundred dollars below the blended numbers shown above.