Most of western Montana and parts of Idaho, Wyoming and South Dakota lost power late Sunday and early yesterday when lightning struck a regional transmission line.

In San Francisco, an unrelated outage left about 21,000 customers without power for nearly two hours when an apparent equipment malfunction caused one transformer to fail and another to explode and burn. Power was restored by 1:46 a.m. to most of the city, but by the start of the business day, about 80 customers in the area still were without power.

The blackout halted some city buses that run on electric overhead wires and darkened some traffic signals, but police reported no incidents.

W. R. Bosshart, chief of systems operations for the Bonneville Power Administration, a federal agency in Portland, Ore., that overseas distribution of hydroelectric power, said yesterday that the outage in Montana. Wyoming, Idaho and South Dakota were triggered when lightning strck a 500,000-volt transmission line connecting four Snake River dams with the BPA substation at the John Day Dam in Oregon. Another line had been knock out Saturday night by a flash flood in central Washington.

Bosshart said a safely mechanism designed to prevent such a disruption failed to work Sunday night.

In Montana, where, Bosshart said, the outage had its greatest impact, officials said yesterday that power had been restored to all areas.