Section 80. If the board of aldermen of a city or the selectmen of a town where a public way and a railroad cross each other and the directors of the railroad corporation deem it necessary for the security and convenience of the public that alteration should be made in such crossing, in the approaches thereto, in the location of the railroad or public way or in the grades thereof, or in a bridge at such crossing, or that such crossing should be discontinued with or without building a new way in substitution therefor, and they agree as to the alterations to be made, a written instrument signed, in behalf of a city, by the mayor, authorized by the board of aldermen, or in behalf of a town, by the chairman of the selectmen, authorized by vote of the town, and by the president of the railroad corporation, authorized by its directors, specifying the manner and limits within which the alterations shall be made, and by which party the work shall be done, or how it shall be apportioned between the city or town and the railroad corporation, the general method of construction, the grades for the railroad and the public way, and also what land or other property it is necessary to take, and what portion, if any, of an existing public way is to be discontinued, and how the cost thereof shall be apportioned between the city or town and the railroad corporation, shall be valid and binding on the city or town and the railroad corporation, respectively, and have the same force and effect as an order of the department of highways under section seventy, if the department of telecommunications and energy, after notice to all parties interested by advertisement and a public hearing, approves of the alterations set forth in the agreement as necessary for the convenience and security of the public; provided, that the department of highways, acting on behalf of the commonwealth, may, if in its judgment it seems advisable, join in such an agreement to abolish any grade crossing, thereby engaging the commonwealth to pay to the parties entitled thereto under the agreement, such amount as said department deems just, and such an agreement in which the commonwealth so joins shall be valid and binding on the commonwealth as well as the other parties thereto, and shall have the same force and effect as an order of said department under said section seventy, after approval as aforesaid by the department of telecommunications and energy. Said agreement, when approved by the department of telecommunications and energy and filed in its office, shall establish the locations as thus altered, and if it is necessary to take land or an easement therein to provide such new locations, the department of highways, in case of the abolition of a grade crossing to the cost of which the commonwealth is to contribute, otherwise the department of telecommunications and energy, shall take the same by eminent domain on behalf of the commonwealth, of the city or town, and of the railroad corporation, respectively, under chapter seventy-nine. Except as otherwise provided in this section, so much of section seventy-four as relates to the taking of land, and so much of section seventy-five as relates to the right of any person to recover damages sustained in consequence of such taking or of the alterations made in pursuance of said order shall apply to the taking of land and to damages sustained under an agreement made pursuant to this section. The crossing and approaches shall be maintained and kept in repair as provided in section seventy-seven, unless the parties agree otherwise. If the agreement provides for the abolition of a grade crossing to the cost of which the commonwealth is to contribute, the department of highways shall keep itself informed of the progress and character of the work and of the amounts reasonably expended for work done or for damages, so far as rendered necessary for the abolition of the grade crossing; and for that purpose it may employ any necessary agents, and, from time to time, as it may consider proper, shall issue certified statements of the amount legally and properly expended for such abolition of a grade crossing. A street railway company or county or other party which would be affected by the alteration of a crossing as aforesaid may join in any agreement under this section.