#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, fu = 0;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar()) (fu |= (ch == '-'));
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  return fu ? -x : x;
}
long long n, fac[1000005], inv[1000005];
long long mo(long long x) { return x >= 998244353 ? x - 998244353 : x; }
long long calc(long long x) { return x < 0 ? x + 998244353 : x; }
long long Q_P(long long x, long long y) {
  long long ret = 1;
  x = calc(x), x = mo(x);
  for (; y; y >>= 1, x = x * x % 998244353)
    if (y & 1) ret = ret * x % 998244353;
  return ret;
}
long long C(long long n, long long m) {
  return fac[n] * inv[m] % 998244353 * inv[n - m] % 998244353;
}
long long ans, Ans, tmp = 1;
int main() {
  n = read(), fac[0] = 1;
  for (long long i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % 998244353;
  inv[n] = Q_P(fac[n], 998244353 - 2);
  for (long long i = n; i >= 1; i--) inv[i - 1] = inv[i] * i % 998244353;
  for (int i = 1; i <= n; i++) {
    int a = 1ll * C(n, i) * Q_P(-1, i + 1) % 998244353;
    int x = Q_P(3, (1ll * n * (n - i) + i) % (998244353 - 1));
    ans = (ans + 1ll * a * x % 998244353) % 998244353;
  }
  ans = 2 * ans % 998244353;
  for (int i = 0; i < n; i++) {
    int t = 998244353 - Q_P(3, i);
    int x = (Q_P(t + 1, n) + 998244353 - Q_P(t, n)) % 998244353;
    int a = 1ll * C(n, i) * Q_P(-1, i + 1) % 998244353;
    Ans = (Ans + 1ll * a * x % 998244353) % 998244353;
  }
  Ans = 3ll * Ans % 998244353;
  printf("%lld\n", (Ans + ans + 998244353) % 998244353);
  return 0;
}
