(Adds prime minister’s quote, defence cooperation)

VILNIUS, June 28 (Reuters) - A deal between Poland’s biggest oil refiner PKN Orlen and Lithuanian Railways agreed on Wednesday on new fees for transporting oil products could pave the way for more cooperation between the two countries on energy and defence.

PKN’s Mazeikiai refinery in Lithuania relies on rail to ship its products to the Klaipeda oil terminal on the Baltic Sea, a route of about 250 kilometres (155 miles), but the Polish company has been at odds with state-run Lithuanian Railways over transport fees since 2014.

Lithuanian Railways and PKN Orlen have now agreed on new tariffs for the cargos and to withdraw all litigation concerning roughly 40 million euros ($45.49 million) in fees unpaid by Orlen since it rejected a fee increase in 2014, a Lithuanian Railways spokesman told Reuters.

Under the agreement, PKN Orlen will have to pay only about half of the debt, head of Lithuanian Railways Mantas Bartuska said.

“Orlen’s troubles were the largest negative factor in our relationship with Poland,” Lithuania’s transport minister Rokas Masiulis said. “As we solved this, we can now expect a breakthrough in all areas, including in energy security,” the minister, who was in charge of Lithuania’s energy security projects in a previous government, told Reuters.

Lithuanian Prime Minister Saulius Skvernelis said the conflict was “casting a shadow on good relationship between our countries.”

“We have a lot of common projects: the energy projects, like the synchronization of our energy grid (with continental Europe), the possible gas link, and then there’s also the strategic defence partnership,” Skvernelis said, asked to list common areas he hopes would be boosted by the agreement.

Poland is the only European Union and NATO member country which shares a border with the Baltic States, making links between Poland and Lithuania vital for regional security as well as energy security.

There are several projects where Lithuania needs to work with Poland, including an expansion of the electric power link between the two countries and a synchronisation of the Baltic countries’ energy grid with the rest of continental Europe.

The two countries are also trying to develop a gas pipeline link.

A source familiar with the matter said the new rail fees would be more favourable for state-run PKN’s Lithuanian subsidiary Orlen Lietuva and could bring it significant savings related to fuel transport from its refinery in Mazeikiai, Lithuania.

“The former agreement did not serve well PKN Orlen and the railways,” head of PKN Orlen Wojciech Jasinski told reporters.

Orlen Lietuva had been a financial burden on its parent. In 2014, PKN wrote down the value of Orlen Lietuva by 4.2 billion zlotys ($1.13 billion) after the refinery swung into the red. It returned to black in 2015, making net profit of $237 million in 2015 and $238 million in 2016.

Jasinski said PKN was looking into reversing the writedown, but could not give a time frame.

“We hope this would go further, some things have to take place. We are having a discussion with the financial market regulator on that,” he said.

In total, PKN has spent $4 billion on the Mazeikiai refinery, including the purchase price, since buying a controlling stake from Russia’s Yukos in 2006. ($1 = 3.7289 zlotys) ($1 = 0.8793 euros) (Reporting By Andrius Sytas. Editing by Jane Merriman)