#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
long long gcd(long long p, long long q) { return q == 0 ? p : gcd(q, p % q); }
int to[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
long long n, ans;
long long qp(long long a, long long b) {
  long long s = 1;
  while (b) {
    if (b & 1) s = s * a % 998244353;
    a = a * a % 998244353;
    b >>= 1;
  }
  return s;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  ans = (qp(3, n * n) - qp(qp(3, n) - 3, n) + 998244353) % 998244353;
  long long xs = -1, c1 = 1;
  for (int i = 1; i <= n; i++) {
    xs *= -1;
    c1 = c1 * (n - i + 1) % 998244353;
    c1 = c1 * qp(i, 998244353 - 2) % 998244353;
    ans += xs *
           (3 * c1 % 998244353 * qp(qp(3, n - i) - 1, n) % 998244353 +
            c1 * (qp(3, i) - 3) % 998244353 * qp(3, (n - i) * n) % 998244353) %
           998244353;
  }
  ans = (ans % 998244353 + 998244353) % 998244353;
  cout << ans << endl;
  return 0;
}
