If the sum of first 7 terms of an AP is 49 and that of last 17 terms is 289, find the sum of its first n terms.