#include <bits/stdc++.h>
using namespace std;
const int _ = 1e6 + 5, yl = 998244353;
long long POW(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1) res = res * x % yl;
    x = x * x % yl;
    y >>= 1;
  }
  return res;
}
long long f[_], mul[_], inv[_], n, ans;
long long C(int x, int y) { return mul[x] * inv[y] % yl * inv[x - y] % yl; }
void inc(long long &x, long long y) {
  x += y;
  if (x >= yl) x -= yl;
}
void dec(long long &x, long long y) {
  x -= y;
  if (x < 0) x += yl;
}
long long F(int x) {
  if (!x) return POW(POW(3, n) - 3, n);
  return C(n, x) *
         ((POW(3, x) - 3) * POW(3, n * (n - x)) % yl +
          3 * POW(POW(3, n - x) - 1, n)) %
         yl;
}
int main() {
  cin >> n;
  inv[0] = inv[1] = mul[0] = 1;
  for (int i = 2; i <= n; ++i) inv[i] = (yl - yl / i) * inv[yl % i] % yl;
  for (int i = 1; i <= n; ++i)
    inv[i] = inv[i] * inv[i - 1] % yl, mul[i] = mul[i - 1] * i % yl;
  ans = POW(3, n * n);
  for (int i = 0; i <= n; ++i)
    if (i & 1)
      inc(ans, F(i));
    else
      dec(ans, F(i));
  cout << ans << endl;
  return 0;
}
